客户端从键盘输入信息,发到服务端,服务端收到信息后,转成大写返回给客户端。
Server
package com.hbsi.net;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(3366);
Socket s=ss.accept();
System.out.println(s.getInetAddress().getHostAddress()+"......connection");
InputStream in = s.getInputStream();
BufferedReader brin=new BufferedReader(new InputStreamReader(in));
BufferedWriter bwout=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line=null;
while((line=brin.readLine())!=null){
if(line.equals("over"))
break;
System.out.println("Client:"+line);
bwout.write(line.toUpperCase());
bwout.newLine();
bwout.flush();
}
s.close();
ss.close();
}
}
Client
package com.hbsi.net;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class TCPClient {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Socket s = new Socket("192.168.49.2", 3366);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bwout = new BufferedWriter(new OutputStreamWriter(s
.getOutputStream()));
BufferedReader brin = new BufferedReader(new InputStreamReader(s
.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
if(line.equals("over"))
break;
bwout.write(line);
bwout.newLine();
bwout.flush();
String str = brin.readLine();
System.out.println("server:" + str);
}
br.close();
s.close();
}
【客户端从键盘输入信息,发到服务端,服务端收到信息后,转成大写返回给客户端。】}
推荐阅读
- Docker应用:容器间通信与Mariadb数据库主从复制
- 一个人的碎碎念
- 我从来不做坏事
- 从蓦然回首到花开在眼前,都是为了更好的明天。
- 西湖游
- 改变自己,先从自我反思开始
- leetcode|leetcode 92. 反转链表 II
- 从我的第一张健身卡谈传统健身房
- 自媒体形势分析
- 操作系统|[译]从内部了解现代浏览器(1)