import java.net.*;
import java.io.*;
class ClientDemo1
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.100",10003);
OutputStream out=s.getOutputStream();
out.write("客户端发送数据".getBytes());
InputStream is=s.getInputStream();
byte[] buf=new byte[1024];
int len=is.read(buf);
String data=new String(buf,0,len);
System.out.println(data);
s.close();
}
}
class ServerDemo1
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10003);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
InputStream is=s.getInputStream();
byte[] buf=new byte[1024];
int len=is.read(buf);
String data=new String(buf,0,len);
System.out.println(ip+"|||"+data);
OutputStream out=s.getOutputStream();
out.write("服务器发送数据".getBytes());
s.close();
ss.close();
}
}
|
|