TCP协议下java通信

Posted 一凡夫一俗子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TCP协议下java通信相关的知识,希望对你有一定的参考价值。

初步,还要改进

服务器

package six.four.five.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;

//:这是一个服务器
public class Sever {
    public static void main(String[] args) {
        //:接受来自客户端的输出流,并读取其中的信息
        List<BufferedWriter> list = new ArrayList<BufferedWriter>();
        try {
            //实现服务器套接字。服务器套接字等待请求通过网络传入。它基于该请求执行某些操作,然后可能向请求者返回结果。 
            ServerSocket server = new ServerSocket(8888);
            System.out.println("服务器已启动");
            while(true) {
                //此类实现客户端套接字(也可以就叫“套接字”)。套接字是两台机器间通信的端点。
                Socket socket = server.accept();
                System.out.println("Client has been linked!");
                //返回此套接字的输入流,也就是客户端的输入流,交给线程并读取其中的数据到
                //InputStream is = socket.getInputStream();
                //将字节流转换为字符流
                InputStreamReader ir = new InputStreamReader(socket.getInputStream());
                //创建缓冲流
                BufferedReader br = new BufferedReader(ir);
                //返回此套接字的输出流,同理是客户端的输出流,交给集合list,要发给每个客户端
                OutputStream os = socket.getOutputStream();
                //将字符流转换为字节流
                OutputStreamWriter ow = new OutputStreamWriter(os);
                //创建缓冲流
                BufferedWriter bw = new BufferedWriter(ow);
                //添加输出流
                list.add(bw);
                //将输入流和集合传到线程中,并执行线程
                new MyThread(br, list).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

服务器的线程

package six.four.five.server;
//:不停的接受客户端发来的消息

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

public class MyThread extends Thread{
    //接受输出流的集合
    private List<BufferedWriter> ow;
    //缓冲流
    private BufferedReader br;
    //线程的构造方法
    public MyThread(BufferedReader br, List<BufferedWriter> ow) {
        this.ow = ow;
        //this.is = new InputStreamReader(is);
        this.br = br;
    }
    @Override
    public  void run() {
        String msg = null;
        while(true) {
            try {
                while(null != (msg = br.readLine())) {
                    //输出字符串
                    System.out.println(msg);
                }
                for(BufferedWriter lo : ow) {
                    //将接受到客户端的信息写到流中,以便发给每个客户端
                    lo.write(msg);
                    lo.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 客户端

package six.four.five.clients;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Clients {
    public static void main(String[] args) {
        try {
            Socket  socket = new Socket(InetAddress.getByName("192.168.0.124"), 8888);
            System.out.println("You has benn linked Sever!");
            //
            InputStream is = socket.getInputStream();
            InputStreamReader ir = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(ir);
            //
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter ow = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(ow);
            //
            Scanner input = new Scanner(System.in);
            System.out.println("What your name?");
            String name = input.nextLine();
            System.out.print("Now,you can say anything:");
            String msg = "";
            new MyThread(br).start();
            while(!(msg.equals("bye"))) {
                //b = msg.getBytes();
                msg = name + ": " + input.nextLine();
                bw.write(msg);
                bw.flush();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端的线程

package six.four.five.clients;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MyThread extends Thread{
    private BufferedReader br;
    public MyThread(BufferedReader br) {
        this.br = br;
    }
    @Override
    public void run() {
        String msg = null;
        while(true) {
            try {
                while(null != (msg = br.readLine())) {
                    System.out.println(msg);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 



以上是关于TCP协议下java通信的主要内容,如果未能解决你的问题,请参考以下文章

阶段1 语言基础+高级_1-3-Java语言高级_07-网络编程_第2节 TCP协议_2_TCP通信的概述(下)

Java网络编程 - TCP通信

阶段1 语言基础+高级_1-3-Java语言高级_07-网络编程_第2节 TCP协议_3_TCP通信的客户端代码实现

-1-7 java 网络编程基本知识点 计算机网络 TCP/IP协议栈 通信必备 tcp udp

java网络通信:TCP协议

JAVA网络编程知识学习