标题:使用TCP编程,事先服务器不断地接收客户端的请求
方式一:客户端
- 先使用BufferedReader,InputStreamReader读取System.in,存到String,
- 再使用DateOutputStream写入到流中【os.writeUTF(str);】
服务器端的完整代码:
//测试System.in,客户端不断获取输入,再写到流中;服务器端接收
/**
* 1.创建ServerSocket(int port)对象
* 2.在Socket上监听客户端的连接请求
* 3.阻塞、等待连接的建立
* 4.接收并处理请求信息
* 5.将处理结果返回给客户端
* 6.关闭流和Socket对象
* @author dell
*
*/
public class TestTCPServerSystemIn02 {
public static void main(String[] args) throws IOException {
System.out.println("-----Server-----");
//1.创建ServerSocket(int port)对象
ServerSocket server=new ServerSocket(6666);
//2.在Socket上监听客户端的连接请求
Socket client=server.accept();
//3.阻塞、等待连接的建立
System.out.println("建立好了一次连接");
//4.接收并处理请求信息
DataInputStream is=new DataInputStream(client.getInputStream());
String str="";
while(!(str=is.readUTF()).equals("exit")){
System.out.println(str);
}
//5.将处理结果返回给客户端
//6.关闭流和Socket对象
System.out.println("接收完毕!!!");
is.close();
server.close();
}
}
客户端的完整代码:
//测试System.in,客户端不断获取输入,再写到流中;服务器端接收
/**
* 1.创建Socket(String host,int port)对象
* 2.向服务器发送连接请求
* 3.向服务器发送服务请求
* 4.接收服务结果
* 5.关闭流和Socket对象
* @author dell
*
*/
public class TestTCPClientSystemIn02 {
public static void main(String[] args) throws IOException {
System.out.println("-----Client-----");
//1.创建Socket(String host,int port)对象
//2.向服务器发送连接请求
Socket client=new Socket("localhost",6666);
//3.向服务器发送服务请求
/*
* 先使用BufferedReader,InputStreamReader读取System.in,存到String,
* 再使用DateOutputStream写入到流中【os.writeUTF(str);】
*/
BufferedReader is=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream os=new DataOutputStream(client.getOutputStream());
String str="";
while((str=is.readLine())!=null) {//使用BufferedReader除去\r\n了
os.writeUTF(str);
os.flush();
if(str.equals("exit")) {
break;
}
}
//4.接收服务结果
//5.关闭流和Socket对象
System.out.println("发送完毕!!!");
os.close();
client.close();
}
}
方式二、
客户端
- 先使用BufferedInputStream读取System.in,存到buf字节数组中
- 【byte[] buf=new byte[1024*60]】
- 再使用DateOutputStream写入到流中【os.writeByte(str);】
服务器端的完整代码:
//测试System.in,客户端不断获取输入,再写到流中;服务器端接收
/**
* 方式一:
* 先使用BufferedReader,InputStreamReader读取System.in,存到String,
* 再使用DateOutputStream写入到流中【os.writeUTF(str);】
* 方式二:
* 先使用BufferedInputStream读取System.in,存到buf字节数组中
* 【byte[] buf=new byte[1024*60]】
* 再使用DateOutputStream写入到流中【os.writeByte(str);】
*/
/**
* 1.创建ServerSocket(int port)对象
* 2.在Socket上监听客户端的连接请求
* 3.阻塞、等待连接的建立
* 4.接收并处理请求信息
* 5.将处理结果返回给客户端
* 6.关闭流和Socket对象
* @author dell
*
*/
public class TestTCPServerSystemIn022 {
public static void main(String[] args) throws IOException {
System.out.println("-----Server-----");
//1.创建ServerSocket(int port)对象
ServerSocket server=new ServerSocket(6666);
//2.在Socket上监听客户端的连接请求
Socket client=server.accept();
//3.阻塞、等待连接的建立
System.out.println("建立好了一次连接");
//4.接收并处理请求信息
DataInputStream is=new DataInputStream(client.getInputStream());
String str="";
while(!(str=is.readUTF()).equals("exit")){
System.out.println(str);
}
//5.将处理结果返回给客户端
//6.关闭流和Socket对象
System.out.println("接收完毕!!!");
is.close();
server.close();
}
}
客户端的完整代码:
//测试System.in,客户端不断获取输入,再写到流中;服务器端接收
/*
* 方式一:
* 先使用BufferedReader,InputStreamReader读取System.in,存到String,
* 再使用DateOutputStream写入到流中【os.writeUTF(str);】
* 方式二:
* 先使用BufferedInputStream读取System.in,存到buf字节数组中
* 【byte[] buf=new byte[1024*60]】
* 再使用DateOutputStream写入到流中【os.writeByte(str);】
*/
/**
* 1.创建Socket(String host,int port)对象
* 2.向服务器发送连接请求
* 3.向服务器发送服务请求
* 4.接收服务结果
* 5.关闭流和Socket对象
* @author dell
*
*/
public class TestTCPClientSystemIn022 {
public static void main(String[] args) throws IOException {
System.out.println("-----Client-----");
//1.创建Socket(String host,int port)对象
//2.向服务器发送连接请求
Socket client=new Socket("localhost",6666);
//3.向服务器发送服务请求
/**
* 方式一:
* 先使用BufferedReader,InputStreamReader读取System.in,存到String,
* 再使用DateOutputStream写入到流中【os.writeUTF(str);】
* 方式二:
* 先使用BufferedInputStream读取System.in,存到buf字节数组中
* 【byte[] buf=new byte[1024*60]】
* 再使用DateOutputStream写入到流中【os.writeByte(str);】
*/
BufferedInputStream is=new BufferedInputStream(System.in);
DataOutputStream os=new DataOutputStream(client.getOutputStream());
byte[] buf=new byte[1024*60];
int len=0;
while((len=is.read(buf))!=-1) {//使用BufferedInputStream没有除去\r\n
String str=new String(buf,0,len-2);
os.writeUTF(str);
os.flush();
if(str.equals("exit")) {
break;
}
}
//4.接收服务结果
//5.关闭流和Socket对象
System.out.println("发送完毕!!!");
os.close();
client.close();
}
}