码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-12-20 23:15:16      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:private   ted   cal   编写   alt   技术分享   todo   row   new t   

技术分享图片

 

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协议编写一个网络程序.....................

标签:private   ted   cal   编写   alt   技术分享   todo   row   new t   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!