描述:基于tcp的Socket与ServerSocket的通信,客户端和服务端的聊天
实现语言:java
一:服务端的代码
1:服务端的键盘录入代码
package chat;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerWrit implements Runnable{
Socket socket;
public ServerWrit(Socket socket){
this.socket=socket;
}
public void run(){
try {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line="";
while((line=br.readLine())!=null){
bw.write(line+"\r\n");
bw.flush();
}
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2:读取客户端发来的信息:
package chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerRead implements Runnable{
Socket socket;
public ServerRead(Socket socket){
this.socket=socket;
}
public void run(){
try {
//获得输入
BufferedReader read=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//进行读取
String str="";
while((str=read.readLine())!=null){
System.out.println("接受到客户端的话:"+str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3:服务端的启动代码:
package chat;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerTest {
public static void main(String[] args) throws Exception {
ServerSocket servers=new ServerSocket(9090);
Socket socket=servers.accept();
new Thread(new ServerRead(socket)).start();
new Thread(new ServerWrit(socket)).start();
//servers.close();
}
}
二:客户端的实现:
1:键盘录入线程
package chat;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ClinckWrit implements Runnable{
Socket socket;
public ClinckWrit(Socket socket){
this.socket=socket;
}
public void run(){
try {
//获得输出流
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//获得输入流
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="";
while((str=br.readLine())!=null){
bw.write(str+"\r\n");
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2:读取客户端的消息线程
package chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ClientRead implements Runnable{
Socket socket;
public ClientRead(Socket socket){
this.socket=socket;
}
public void run(){
try {
BufferedReader br =new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str="";
while((str=br.readLine())!=null){
System.out.println("服务端回应:"+str);
}
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3:启动客户端的程序
package chat;
import java.net.InetAddress;
import java.net.Socket;
public class ClientT {
public static void main(String[] args) throws Exception {
Socket socket=new Socket(InetAddress.getLocalHost(),9090);
new Thread(new ClinckWrit(socket)).start();
new Thread(new ClientRead(socket)).start();
}
}
先启动服务端,然后再启动客户端,这样就可以愉快地聊天了
稍作改动就可以做成群聊了