1.简单介绍
我们在了解TCp双向通信后知道双向通信说白了就是在一个线程中进行的,必须要有逻辑顺序,首先要启动服务端,然后启动客户端,接着客户端发送消息到服务端,服务端接收到消息显示到控制台上,然后服务端发送消息到客户端,客户端接收到消息后才能进行再一次的输入,这就是一次完整的过程。
接下来我么实现的就是简单的微信聊天软件,你可以发送多条信息给对方也可以接受多条信息。此处用到了多线程知识。
2.代码展示
1.客户端
package webCoding;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Objects;
import java.util.Scanner;
//客户端单点
public class ChatSocketClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("192.168.124.2",8888);
System.out.println("连接成功!!");
new ClientSend(socket).start();
new ClientReceive(socket).start();
}catch (Exception e) {
System.out.println("连接失败........");
}
}
}
class ClientSend extends Thread{
private Socket socket;
public ClientSend(Socket socket)
{
this.socket = socket;
}
public void run()
{
this.sendMsg();
}
//客户端发送消息线程
private void sendMsg() {
try(Scanner scanner = new Scanner(System.in);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream())){
while(true)
{
String tmp = scanner.nextLine();
printWriter.println(tmp);
printWriter.flush();
if (Objects.equals("exit",tmp))
{
System.out.println("结束聊天");
break;
}
}
}catch (Exception e)
{
System.out.println("发送失败");
}
}
}
class ClientReceive extends Thread
{
private Socket socket;
public ClientReceive(Socket socket)
{
this.socket = socket;
}
public void run()
{
this.receiveMsg();
}
//客户端接收消息线程
private void receiveMsg() {
try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())))
{
while (true)
{
String temp = bufferedReader.readLine();
System.out.println("服务端:"+temp);
if (Objects.equals("exit",temp))
{
System.out.println("对方已结束聊天");
break;
}
}
}catch (Exception e)
{
System.out.println("接受失败.....");
}
}
}
2.服务端
package webCoding;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Objects;
import java.util.Scanner;
//服务端单点
public class ChatSocketServer {
public static void main(String[] args) {
try(ServerSocket serverSocket = new ServerSocket(8888)){
System.out.println("服务端启动,等待连接..........");
Socket socket =serverSocket.accept();
System.out.println("连接成功!!");
new Send(socket).start();
new Receive(socket).start();
}catch(Exception e)
{
System.out.println("服务端创建失败.........");
}
}
}
class Send extends Thread{
private Socket socket;
public Send(Socket socket)
{
this.socket = socket;
}
public void run()
{
this.sendMsg();
}
//服务端发送消息线程
public void sendMsg() {
try(PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
Scanner scanner = new Scanner(System.in);)
{
while (true) {
String tmp = scanner.nextLine();
printWriter.println(tmp);
printWriter.flush();
if (Objects.equals("exit",tmp)) {
System.out.println("结束通话");
break;
}
}
}catch (Exception e)
{
System.out.println("消息发送失败....");
}
}
}
class Receive extends Thread{
private Socket socket;
public Receive(Socket socket) {
this.socket = socket;
}
public void run() {
this.receiveMsg();
}
//服务端接收消息线程
private void receiveMsg() {
try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()))){
while (true) {
String temp = bufferedReader.readLine();
System.out.println("客户端:"+temp);
if (Objects.equals(temp,"exit"))
{
System.out.println("对方结束通话");
break;
}
}
}catch (Exception e)
{
System.out.println("消息接受失败........");
}
}
}
3.运行结果
结果如我们所料,我们简单的实现了微信聊天,上面的代码我们一个文件中有好几个类,但是我介意宝贝们一个类一个文件一个线程,否则可能会出现不必要的错误或bug。
最后就希望喜欢的宝子们点个赞啦!