黑马程序员技术交流社区
标题:
TCP协议
[打印本页]
作者:
pisces
时间:
2015-5-10 21:54
标题:
TCP协议
使用TCP协议完成一个客户端一个服务器。客户端从键盘输入读取一个字符串,发送到服务器。
服务器接收客户端发送的字符串,反转之后发回客户端。客户端接收并打印
作者:
xiaodaodan
时间:
2015-5-10 22:09
package cn.itcast.test; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Server { public static void main(String[] args) throws Exception { ServerSocket socket = new ServerSocket(9999) ; Socket accept = socket.accept() ; InputStream inputStream = accept.getInputStream() ; BufferedReader buf = new BufferedReader(new InputStreamReader(inputStream)); PrintWriter pw = new PrintWriter(accept.getOutputStream() , true) ; String line = null; while((line = buf.readLine()) != null){ byte[] bytes = line.getBytes() ; reverseArr(bytes); pw.println(new String(bytes )); } accept.close() ; socket.close() ; } public static void reverseArr(byte[] arr){ for(int x = 0 , y = arr.length - 1 ; x <= y ; x++ , y--){ byte temp = arr[x]; arr[x] = arr[y] ; arr[y] = temp ; } } }
作者:
xiaodaodan
时间:
2015-5-10 22:11
package cn.itcast.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Server {
public static void main(String[] args) throws Exception {
ServerSocket socket = new ServerSocket(9999) ;
Socket accept = socket.accept() ;
InputStream inputStream = accept.getInputStream() ;
BufferedReader buf = new BufferedReader(new InputStreamReader(inputStream));
PrintWriter pw = new PrintWriter(accept.getOutputStream() , true) ;
String line = null;
while((line = buf.readLine()) != null){
byte[] bytes = line.getBytes() ;
reverseArr(bytes);
pw.println(new String(bytes ));
}
accept.close() ;
socket.close() ;
}
public static void reverseArr(byte[] arr){
for(int x = 0 , y = arr.length - 1 ; x <= y ; x++ , y--){
byte temp = arr[x];
arr[x] = arr[y] ;
arr[y] = temp ;
}
}
}
还有一个jar包...
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2