套接字问题 - 只读取第一条消息

时间:2012-11-10 11:47:15

标签: java sockets

我对套接字很新,并希望有人可以帮助我。我有一些工作,但它不是很快发送信息所以我已经重构,现在无法回到任何有效的东西。问题似乎是只读取了发布的第一条消息,然后接收方位于client = listener.accept();,即使我非常确定发送方仍在发送消息

有人能看到我在这里做错了吗?

由于

  public class Sender {
        Socket server = null;
        DataInputStream inp = null;
        PrintStream outp = null;

        public Sender(){
            server = new Socket("127.0.0.1" , 3456);
            outp = new PrintStream(server.getOutputStream());
        }

        private void connectAndSendToServer(String message)  {
            outp = new PrintStream(server.getOutputStream());
            outp.print(message + "\n");
            outp.flush();

        }
    }

接收者类

public class Receive{


    public String receiveMessage(int port) {
        String message= null;
        ServerSocket listener = null;
        Socket client = null;
        try{
            listener = new ServerSocket(port);
            client = listener.accept();         
            BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
            return br.readLine();

        }
        ...
        finally{
            try {
                if(client!=null && listener!=null){
                    client.close();
                    listener.close();
                }
            } 
            catch (IOException e) {

            }
        }
        return message;
    }
}

3 个答案:

答案 0 :(得分:2)

这是因为ServerSocket用作普通Socket的入口点。 accept()是一种阻塞操作,与接收/发送数据到普通Socket的线程相比,通常在不同的线程上完成。它坐在那里等待一个新的连接产生一个新的Socket,然后用于数据。

这意味着在接收消息时,您应该只调用readLine()来读取特定的Socket。在accept中设置receiveMessage是错误的,因为它是一个不同的操作,甚至是阻塞。

Socket socket = serverSocket.accept();

ClientThread thread = new ClientThread(socket);

class ClientThread extends Thread {
  Socket socket;

  public void run() {
    while (!closed) {
      String line = reader.readLine();
      ...
    }
  }

您不需要为每个客户端都有一个线程,但是如果您想让服务器接受大于1的连接数,则至少需要两个。

答案 1 :(得分:2)

您没有正确使用ServerSocket。您不应该为每条消息创建一个新实例,而是将其用作数据成员,并运行无限循环以获得新的客户端套接字连接。因为您在本地创建它,所以套接字是关闭的,因为当您从方法返回时,不再使用和引用对象(以及GC'ed)。
类似于(< condition met>是伪代码定义了接受新连接的条件):

while(< condition met >) {
    try {
        client = listener.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String str = br.readLine();
        //do something with str
    } finally {
        //close client socket
    }    
}

更好的方法是在另一个线程中处理客户端套接字,这样主线程就可以恢复接受了,而你可以并行地对客户端套接字做任何事情。

答案 2 :(得分:0)

- 试试我写的这个基本聊天服务器。此服务器只是继续在循环中运行,并将客户端发送的消息广播到与此服务器关联的所有其他客户端。

- 我希望此代码对您有所帮助。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class Server {

    // ///----------------------------------------Instance Variable Fields
    ServerSocket ss = null;
    Socket incoming = null;

    // ///----------------------------------------Instance Variable Fields

    // ///---------------------------------------- static Variable Fields
    public static ArrayList<Socket> socList = new ArrayList<Socket>();

    // ///---------------------------------------- static Variable Fields

    public void go() {

        try {

            ss = new ServerSocket(25005);

            while (true) {

                incoming = ss.accept();
                socList.add(incoming);
                System.out.println("Incoming: " + incoming);
                new Thread(new ClientHandleKaro(incoming)).start();

            }

        } catch (IOException e) {

            e.printStackTrace();
        } finally {

            try {
                ss.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    class ClientHandleKaro implements Runnable {

        InputStream is = null;
        OutputStream os = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        PrintWriter pw = null;
        boolean isDone = false;

        Socket sInThread = null;

        public ClientHandleKaro(Socket sxxx) {

            this.sInThread = sxxx;

        }

        @Override
        public void run() {

            if (sInThread.isConnected()) {

                System.out.println("Welcamu Clienta");
                System.out.println(socList);
            }

            try {
                is = sInThread.getInputStream();
                System.out.println("IS: " + is);
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);

                os = sInThread.getOutputStream();
                pw = new PrintWriter(os, true);

                String s = new String();

                while ((!isDone) && (s = br.readLine()) != null) {

                    String[] asx = s.split("-");
                    System.out.println("On Console: " + s);

                    // pw.println(s);

                    Thread tx = new Thread(new ReplyKaroToClient(s,
                            this.sInThread));
                    tx.start();

                    if (asx[1].trim().equalsIgnoreCase("BYE")) {

                        System.out.println("I am inside Bye");
                        isDone = true;

                    }
                }
            } catch (IOException e) {

                System.out.println("Thanks for Chatting.....");
            } finally {

                try {
                    Thread tiku = new Thread(new ByeByeKarDo(sInThread));
                    tiku.start();
                    try {
                        tiku.join();
                    } catch (InterruptedException e) {

                        e.printStackTrace();
                    }

                    System.out.println("Accha to hum Chalte hain !!!");
                    System.out.println(socList);

                    br.close();
                    pw.close();
                    sInThread.close();

                } catch (IOException e) {

                }
            }

        }

    }

    class ReplyKaroToClient implements Runnable {

        public String mString;
        public Socket mSocket;

        public ReplyKaroToClient(String s, Socket sIn) {

            this.mString = s;
            this.mSocket = sIn;
        }

        @Override
        public void run() {

            for (Socket sRaW : socList) {

                if (mSocket.equals(sRaW)) {
                    System.out.println("Mai same hun");
                    continue;

                } else {
                    try {
                        new PrintWriter(sRaW.getOutputStream(), true)
                                .println(mString);
                    } catch (IOException e) {

                        System.out.println("Its in Catch");

                    }
                }
            }

        }

    }

    class ByeByeKarDo implements Runnable {

        Socket inCom;

        public ByeByeKarDo(Socket si) {

            this.inCom = si;
        }

        @Override
        public void run() {

            try {
                new PrintWriter(inCom.getOutputStream(), true)
                        .println("You have Logged Out of Server... Thanks for ur Visit");
            } catch (IOException e) {

                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {

        new Server().go();
    }
}