基于tcp协议的socket客户端与服务端的通信-java

描述:基于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();
    }
}

先启动服务端,然后再启动客户端,这样就可以愉快地聊天了
稍作改动就可以做成群聊了

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值