套接字写作无响应

时间:2013-02-25 14:08:44

标签: java sockets

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

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

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

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

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

2 个答案:

答案 0 :(得分: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);
        }        
    }
}

答案 1 :(得分: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);
    }
相关问题