标签:方式 put while 输入 puts accept new 客户 socket
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha
客户端
Socket socket = new Socket("ip", 端口);
InputStream is = socket.getInputStream();
DataInputStream dis = new DataInputStream(is);
OutputStream os = socket.getOutputStream();
DataInputStream dos = new DataOutputStream(os);
服务器端
ServerSocket serverSocket = new ServerSocket(端口);
Socket socket = serverSocket.accept();
//获取流的方式与客户端一样
读取输入流
byte[] buffer = new byte[1024];
do{
int count = is.read(buffer);
if(count <= 0){ break; }
else{
// 对buffer保存或者做些其他操作
}
}
while(true);
客户端和服务器端一样的
DatagramSocket socket = new DatagramSocket(端口);
InetAddress serverAddress = InetAddress.getbyName("ip");
//发送
DatagramPackage packet = new DatagramPacket(buffer, length, host, port);
socket.send(packet);
//接收
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, 1024);
Socket.receive(packet);
标签:方式 put while 输入 puts accept new 客户 socket
原文地址:http://www.cnblogs.com/yue31313/p/7400905.html