-3

我做了一个简单的应用程序来连接服务器和java中的客户端它不起作用

我使用 new Socket("localhost", 4444); 对于客户端和新的 ServerSocket(4444);为服务器

客户: http ://www.hastebin.com/siqamonebu.coffee

服务器: http ://www.hastebin.com/rivebetani.avrasm

我在 hastebin 上发布了代码,因为它在这里发布太大了我不知道哪个部分坏了,但是服务器从来没有收到你说的消息

4

2 回答 2

2

ChatServer - 向所有连接的客户端广播

在一个命令提示符中:java ChartServer
在另一个:(java ChatClient localhost 或服务器运行位置的 IP 地址)
另一个:(java ChatClient localhost 或服务器运行位置的 IP 地址)

开始在客户端窗口中聊天。

像这样的服务器...

// xagyg wrote this, but you can copy it
import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {

    public static List list = new ArrayList();

    public static void main(String[] args) throws Exception {

        ServerSocket svr = new ServerSocket(4444);

        System.out.println("Chat Server started!");

        while (true) {
            try {
                Socket s = svr.accept();
                synchronized(list) {
                   list.add(s);              
                }                                  
                new Handler(s, list).start();
            }
            catch (IOException e) {
                // print out the error, but continue!
                System.err.println(e);
            }
        }
    }
}

class Handler extends Thread {

    private Socket s;
    private String ipaddress;
    private List list;

    Handler (Socket s, List list) throws Exception {
      this.s = s;
      ipaddress = s.getInetAddress().toString();
      this.list = list;
    }

    public void run () {

      try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String message;
        //MyDialog x = (MyDialog)map.get(ipaddress.substring(1));
        while ((message = reader.readLine()) != null) {
            if (message.equals("quit")) {
                synchronized(list) {
                    list.remove(s);
                }
                break;
            }
            synchronized(list) {
                for (Object object: list) {
                    Socket socket = (Socket)object;
                    if (socket==s) continue;
                    PrintWriter writer = new PrintWriter(socket.getOutputStream());
                    writer.println(ipaddress + ": " + message);
                    writer.flush();
                }
            }
        }
        try { reader.close(); } catch (Exception e) {}
      }
      catch (Exception e) {
        System.err.println(e);
      }
    }
}

这样的客户...

// xagyg wrote this, but you can copy it
import java.util.*;
import java.io.*;
import java.net.*;

public class ChatClient {


    public static void main(String[] args) throws Exception {

        Socket s = new Socket(args[0], 4444);
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter out = new PrintWriter(s.getOutputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String message;
        new SocketReader(in).start();   
        while ((message = reader.readLine())!=null) {
            out.println(message);
            out.flush();
            if (message.equals("quit")) break;
        }
        in.close();
        out.close();
    }        
}

class SocketReader extends Thread {

    BufferedReader in;

    public SocketReader(BufferedReader in) {
        this.in = in;
    }

    public void run() {   
        String message;
        try {
            while ((message = in.readLine())!=null) {
                System.out.println(message);
            }
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }        
    }
}
于 2013-02-25T15:06:29.130 回答
0

我认为这是因为您如何启动服务器,尝试使用这种结构,因为您在 run 方法中创建 ServerSocket,这可能会导致冲突:

public class ThreadSocketServer implements Runnable {

    private int port;
    private boolean isRunning;
    private ServerSocket ss;

    public ThreadSocketServer(int port, boolean initialStatus) {
        this.port = port;
        isRunning = initialStatus;
        System.out.println("Start the server!");
        try {
            ss = new ServerSocket(port);
            Thread threadServer = new Thread(this);
            threadServer.start();
        } catch (IOException e) {
            System.err.println(e.getClass() + " - " + e.getMessage());
        }
    }

    @Override
    public void run() {

        while (isRunning) {
            try {
                Socket client = ss.accept();

                ObjectInputStream ois = new ObjectInputStream(
                        client.getInputStream());
                //Process here
                ois.close();
            } catch (IOException e) {
                System.err.println(e.getClass() + " - " + e.getMessage());
                isRunning = false;
            } catch (ClassNotFoundException e) {
                System.err.println(e.getClass() + " - " + e.getMessage());
                isRunning = false;
            }
        }

    }

    public static void main(String[] args) {
        new ThreadSocketServer(1085, true);
    }
于 2013-02-25T14:16:55.823 回答