socket编程(一),实现服务器与客户端简单通信

本节主讲客户端向服务发送信息,服务器转发给客户端,当然也可以稍微改一下代码可以实现互传消息,不再赘述。
难点在于服务器端的代码思路:
(1)主程序Socket socket=server.accept();等待客户端连接。
(2)创建线程函数,实现连接成功后创建一个新的线程,服务器转发客户端消息。
TCPServer.java

package com.ysu.server.chatroom;

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


public class TCPServer  { 
    private int port=4443;//默认服务器端口
    public void setPort(int port){
        this.port=port;
    }
    public int getPort(){
        return this.port;
    }
    public void servers(){
        try {
            ServerSocket server=new ServerSocket(this.getPort());
            int i=0;
            while(true){
                //等待客户端连接
                Socket socket=server.accept();
                i++;
                System.out.println("第"+i+"个客户端连接!");
                //连接成功后创建一个新的线程实现服务器通信服务
                new Thread(new ThreadServer(socket)).start();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException  {
        new TCPServer().servers();
    }

}

ThreadServer.java

package com.ysu.server.chatroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ThreadServer implements Runnable{
    private Socket socket;

    public ThreadServer(Socket socket){
        this.socket=socket;

    }
    public Socket getSocket(){
        return this.socket;
    }
    //多线程运行函数
    public void run() {
        BufferedReader in= null;
        PrintWriter write = null;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        try {
            //获取输出流
            write=new PrintWriter(this.socket.getOutputStream());
            //获取输入流
            in=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            String str=in.readLine();

            while(!(str==null||str.equals("end")||str.equals(""))){             
            System.out.println(str);
            write.println("服务器(转)"+str);
            write.flush();//强制刷新缓冲区,将缓冲区全部输出
            str=in.readLine();
            }   

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                //关闭流
                write.close();
                in.close();
                this.getSocket().close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

以上是服务器端的代码,下面是客户端代码比服务器简单。
TCPClient1.java

package com.ysu.server.chatroom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class TCPClient1 {

    public static void main(String[] args) {
        try {
            Socket socket=new Socket("127.0.0.1",4443);
            System.out.println("客户1:服务已经连接......");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            PrintWriter write=new PrintWriter(socket.getOutputStream());
            BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));

            //System.out.println(in.readLine());
            String line=br.readLine();
            //System.out.println(line);
            while(!line.equals("end")){
                //System.out.println(line);
                write.println("客户端1:"+line);
                write.flush();
                System.out.println(in.readLine());
                line=br.readLine();
            }
            write.close();
            in.close();
            br.close();
            socket.close();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

运行效果图:
socket编程(一),实现服务器与客户端简单通信_第1张图片

你可能感兴趣的:(java,socket,多线程)