Java之Socket编程之简单聊天程序

一、界面截图:




二、代码:


1、ChatServer.java

package com.lixh1986.socket.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;

import com.lixh1986.socket.model.ServerSideSocket;

public class ChatServer {
    
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        
        ArrayList<ServerSideSocket> pool = new ArrayList<ServerSideSocket>(); 
        
        try {
            
            ServerSocket serverSocket = new ServerSocket(9999);
            
            System.out.println("Server starts up!");
            
            while(true){
                /**
                 * ServerSocket.accept();
                 *
                 Listens for a connection to be made to this socket and accepts it. 
                 The method blocks until a connection is made. 

                 A new Socket corresponding to the client socket is created, and this
                 socket will be used for making communication with the client socket. 
                 
                 */
                ServerSideSocket serverSideSocket = 
                    new ServerSideSocket(pool, serverSocket.accept());
                Thread t = new Thread(serverSideSocket);
                System.out.println("socket thread: " + t.getName() + " starts serving.");
                t.start();
            }
            
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

}




2、ServerSideSocket.java

package com.lixh1986.socket.model;

import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;

public class ServerSideSocket implements Runnable {
    
    private Socket socket;    
    private ArrayList<ServerSideSocket> pool;
    
 
    public ServerSideSocket(ArrayList<ServerSideSocket> pool, Socket socket){
        this.pool = pool;
        this.socket = socket;
        this.pool.add(this);
        System.out.println("A client connected!");
    }
    
    
    @Override
    public void run() {
        receiveMessage();
    }
    
    /**
     * read data from ClientSideSocket.
     */
    private void receiveMessage(){
        try {
            byte[] bytes = new byte[1024];
            int n = 0;
            boolean isUserSendQuit = false;
            while(!isUserSendQuit){
                while ( (n = socket.getInputStream().read(bytes)) != -1){
                    String str = new String (bytes, 0, n);
                    // broadcast this message to other client.
                    for(ServerSideSocket otherSocket : pool){
                        if(this != otherSocket){
                            otherSocket.writeMessage(str);
                        }
                    }
                }
            }
        } catch (IOException e1) {
            System.out.println("receiveMessage:");
            System.out.println("A client disconnected!");
        }finally{
            disconnect();
        }
    }
    
    
    /**
     * write data to ClientSideSocket.
     * @param message
     */
    public void writeMessage(String message){
        try {
            socket.getOutputStream().write(message.getBytes());
            socket.getOutputStream().flush();
        } catch (IOException e1) {
            System.out.println("writeMessage:");
            System.out.println("A client disconnected!");
            disconnect();
        }
    }
   
    
    private void disconnect(){
        if(pool.remove(this)){
            System.out.println("A client is removed successfully!");
            System.out.println("pool size: " + pool.size());
        }else{
            System.out.println("A client is not removed!");
        }
        try {
            if(socket != null) socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}






3、ClientSideSocket.java

package com.lixh1986.socket.client;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

@SuppressWarnings("resource")
public class ClientSideSocket {
   
    public static void main(String[] args) {
        try {
            
            final Socket socket = new Socket("127.0.0.1", 9999);
            final Scanner scanner = new Scanner(System.in);
            
            System.out.println("I am a client!");
            
       
            new Thread(new Runnable() {
                @Override
                public void run() {                        
                    try {
                        while(true){
                            String str = scanner.nextLine();
                            socket.getOutputStream().write(str.getBytes());
                            socket.getOutputStream().flush();
                        }
                    } catch (IOException e) {
                        System.out.println("Writing Quit.");
                        System.exit(0);
                    }
                }
            }).start();
            
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        byte[] bytes = new byte[1024];
                        int n = 0;
                        while(true){
                            while ( (n = socket.getInputStream().read(bytes)) != -1){
                                String str = new String (bytes, 0, n);
                                System.out.println(str);
                            }
                        }                        
                     } catch (IOException e) {
                         System.out.println("Reading Quit.");
                         System.exit(0);
                     }
                }
            }).start();
            
            
            
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    

}






三、Java Socket通信原理图






四、Java Socket 编程进阶


Will I need a separate socket and thread for every player that joins? [JAVA]
服务器端是否应该为每一个客户端创建一个线程?

I have been learning about sockets for sometime now (I'm quite young) and I think I have a good grip on java sockets. I have decided to create a simple multiplayer Java 2D social game. My goal is to have the server output players' X,Y coordinates and chat every 10 milliseconds. From what I have read, my very average logic tells me that only one user at a time can connect to a socket. So therefore I will need a separate thread and socket for each player that connects.

Is it necessary to have one ServerSocket and thread per player?


Answer:

You should have just one ServerSocket listening on a port that is known to the client. When a client connects to the server, a new Socket object is created and the original ServerSocket goes back to listening again. You should then spin off a new Thread or hand over to an Executor the actual work of talking to the client, otherwise your server will stop listening for client connections.

Here is a very basic sketch of the code you will need.


import java.net.*;
import java.util.concurrent.*;

public class CoordinateServer {
  public static void main(String... argv) throws Exception {
    // 'port' is known to the server and the client
    int port = Integer.valueOf(argv[0]);
    ServerSocket ss = new ServerSocket(port);

    // You should decide what the best type of service is here
    ExecutorService es = Executors.newCachedThreadPool ();

    // How will you decide to shut the server down?
    while (true) {
      // Blocks until a client connects, returns the new socket 
      // to use to talk to the client
      Socket s = ss.accept ();

      // CoordinateOutputter is a class that implements Runnable 
      // and sends co-ordinates to a given socket; it's also
      // responsible for cleaning up the socket and any other
      // resources when the client leaves
      es.submit(new CoordinateOutputter(s));
    }
  }
}





I have put sockets here since they are easier to get started with, but once you have this working well and want to boost your performance you will probably want to investigate the java.nio.channels package.

There's a good tutorial over at IBM.





-
转载请注明,
原文出处: http://lixh1986.iteye.com/blog/2348001









-

猜你喜欢

转载自lixh1986.iteye.com/blog/2348001