您的位置:首页 > 理论基础 > 计算机网络

java中TCP传输协议

2015-08-13 13:15 323 查看
class TcpClient {

public static void main(String[] args) throws Exception {
//创建客户端的socket服务,指定目的主机和端口
Socket s = new Socket("192.168.1.10",10003);
//为了发送数据,获取socket流中的输出流
java.io.OutputStream out = s.getOutputStream();
out.write("hello tcp".getBytes());
s.close();
}
}

class TcpServer {

public static void main(String[] args) throws Exception {
//建立服务端socket服务,并监听一个端口
ServerSocket ss = new ServerSocket(10003);
//通过accept方法获取连接过来的客户端对象
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
//获取客户端发送过来的数据,使用客户端对象的读取流来读取数据
InputStream in = s.getInputStream();

byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
//关闭客户端
s.close();

}
}

class TcpClient2 {

public static void main(String[] args) throws Exception {
//建立socket服务,指定连接的主机和端口
Socket s = new Socket("192.168.1.10",10004);
//获取socket流中的输出流。将数据写入该流,通过网络传送给服务端
OutputStream out = (OutputStream) s.getOutputStream();
out.write("hello Tcp".getBytes());
//获取socket流中的输入流,将服务端反馈的数据获取到,并打印
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
s.close();

}
}

class TcpServer2 {

public static void main(String[] args) throws Exception {

ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
//服务端收信息
InputStream in = s.getInputStream();
String ip = s.getInetAddress().getHostAddress();

byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));

//服务端发信息
OutputStream out = (OutputStream) s.getOutputStream();
out.write("have receive".getBytes());
s.close();
ss.close();

}
}

/*需求:建立一个文本转换服务器
客户端给服务端发送文本,服务端会将文本转成大写再返还给客户端*/

class TcpClient3 {

public static void main(String[] args) throws Exception {
Socket s = new Socket("192.168.1.4",10005);
//定义读取键盘数据的流对象,读取键盘录入文本
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

//获取socket流中的输出流,通过网络传送到服务端
BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

//定义一个socket读取流,读取服务端返回的大写信息
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));

String line = null;
while((line=bufr.readLine())!=null) {
if("over".equals(line))
break;
bufOut.write(line);
bufOut.newLine();//结束标记
bufOut.flush();

String str = bufIn.readLine();
System.out.println("server:"+str);
}
bufr.close();
s.close();
}
}

class TcpServer3 {

public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10005);
Socket s = ss.accept();
//读取socket读取流中的数据
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));

//socket输出流,将大写文本写入到socket输出流,并发送给客户端
BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

String line = null;
while((line=bufIn.readLine())!=null) {
bufOut.write(line.toUpperCase());
bufOut.newLine();//结束标记
bufOut.flush();
}
s.close();
ss.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java TCP