首页 > 其他 > 详细

使用TCP协议编写一个网络程序.....................

时间:2017-12-20 23:06:49      阅读:240      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

package tCP;

import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;

public class TCPClient {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new TCPClient1().connect();


}

}
class TCPClient1{
private static final int PORT = 8002;
public void connect() throws Exception{
Socket client = new Socket(InetAddress.getLocalHost(), PORT);
InputStream is = client.getInputStream();
byte[] buf = new byte[1024];
int len = is.read(buf);
System.out.println(new String(buf, 0, len));
client.close();
}
}

 

package tCP;


import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new TCPServer1().listen();

}

}

class TCPServer1{
private static final int PORT = 8002;
public void listen() throws Exception{
ServerSocket serverSocket = new ServerSocket(PORT);
Socket client = serverSocket.accept();
OutputStream os = client.getOutputStream();
System.out.println("开始与客户端交互数据");
String str = "Hello,world";
os.write(str.getBytes());
System.out.println("结束与客户端交互数据");
os.close();
client.close();
}
}

技术分享图片

技术分享图片

使用TCP协议编写一个网络程序.....................

原文:http://www.cnblogs.com/lyly01/p/8076246.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!