一个用Java写的简单的TCP聊天程序

服务端代码:

package com.test.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	
	public static void main(String[] args) {
		new Server().startServer();
	}
	
	public void startServer(){
		try {
			//服务器在9990端口监听客户端的连接
			ServerSocket ss = new ServerSocket(9999);
			System.out.println("server is listening...");
			while(true){
				//阻塞的accept方法,当一个客户端连接上,才会返回Socket对象
				Socket s = ss.accept();
				System.out.println("a client has connected!");
				//开启线程处理通信
				new CommunicateThread(s).start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public class CommunicateThread extends Thread{
		
		Socket socket;
		DataInputStream dis;
		DataOutputStream dos;
		
		public CommunicateThread(Socket socket){
			this.socket = socket;
			try {
				dis = new DataInputStream(socket.getInputStream());
				dos = new DataOutputStream(socket.getOutputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		public void run() {
			super.run();
			//读取客户端发过来的消息
			String msg = null;
			try {
				while((msg = dis.readUTF()) != null){
					System.out.println("client send msg :" + msg);
					String replyMsg = "server reply : " + msg;
					dos.writeUTF(replyMsg);
					System.out.println("server reply msg :" + replyMsg);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
客户端代码:

package com.test.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class Client {
	
	public static void main(String[] args) {
		new Client().startClient();
	}
	
	public void startClient(){
		try {
			//连接到服务器
			Socket socket = new Socket("localhost", 9999);
			DataInputStream dis = new DataInputStream(socket.getInputStream());
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			Scanner scanner = new Scanner(System.in);
			String line = null;
			listenServerReply(dis);
			while((line = scanner.nextLine()) != null){//读取从键盘输入的一行
				dos.writeUTF(line);//发给服务端
				System.out.println("client send msg : " + line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//监听服务端回复的消息
	public void listenServerReply(final DataInputStream dis){
		new Thread(){
			@Override
			public void run() {
				super.run();
				String line = null;
				try {
					while((line = dis.readUTF()) != null){
						System.out.println("client receive msg from server: " + line);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

}
先启动服务端,再启动客户端,在客户端的控制台输入消息并回车,就可以发送消息给客户端了,客户端收到消息后,马上会回复一个消息


  • 0
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yubo_725

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值