新建Java工程:
1.服务器:
try {
//创建套接字,ip,端口号
ServerSocket socket = new ServerSocket(8801);
System.out.println("socket套接字创建成功");
//客户端接入
while(true){
final Socket con = socket.accept();
System.out.println("客户端接入");
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
InputStream in;
try {
byte[] data = new byte[128];
int len = 0;
//获取输入流,用来读取数据
in = con.getInputStream();
len = in.read(data);
System.out.println("read data:"+new String(data,0,len));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2.客户端:
try {
Socket client = new Socket("192.168.1.3",8801);
//获得数据发送通道
OutputStream out = client.getOutputStr