0

您好,我是 Java 套接字编程的新手,我只是想看看是否有人可以给我一些帮助。

我将发布客户端和服务器的代码,然后我将解释我的问题......

            reader = new BufferedReader(new InputStreamReader(socket.getInputStream));

        while(running)
        {
            String line = reader.readLine();

            if(line != null)
            {
                System.out.println(line);
                stream = new PrintStream(socket.getOutputStream());
                stream.println("return: " + line);
            }

        }

    }catch(IOException e)
    {
        System.out.println("Socket in use or not available: " + port);
    }
}

public static void main()
{
    run();
}


//Client

public static String ip;
public static int port;

public static Socket socket;
public static PrintStream stream;
public static BufferedReader reader;

public static void main(String args[])
{

    try
    {
        socket = new socket(ip, port);
        stream = new PrintStream(socket.getOutputStream());
        stream.println("test0");
        reader = new BufferedReader(new  InputStreamReader(socket.getInputStream));
        String line = reader.readLine();

        if(line != null)
        {
            System.out.println(line);
        }   

        stream.println("test1");
        line = reader.readLine();

        if(line != null)
        {
            System.out.println(line);   
        }   
    }catch(IOException e)
    {
            System.out.println("could not connect to server!");
    }
}

所以我的问题是,即使我摆脱了循环并尝试让它发送两次字符串,它也不会发送它。除非我关闭并在客户端创建一个新套接字,否则它只会执行一次。因此,如果有人能给我解释我做错了什么,那就太好了,非常感谢。

4

4 回答 4

1

为什么要在循环内打开外流? stream = new PrintStream(socket.getOutputStream());

将此语句放在循环之外并写入您stream的循环内部。

于 2012-09-24T16:18:23.623 回答
0

非常感谢所有回答的人,但我知道它一直都是什么。我通读了一些 Oracle 套接字的东西,发现服务器应该是第一个发送消息的人,而不是客户端接收和发送和接收......等等,所以我将在这里发布我的新代码希望其他试图弄清楚同样事情的人可以轻松找到它

    //Client    
public static String ip;
public static int port;

public static Socket socket;
public static PrintWriter print;
public static BufferedReader reader;

public Client(String ip, int port)
{
    this.ip = ip;
    this.port = port;

            //initiate all of objects
    try 
    {
        socket = new Socket();
        socket.connect(new InetSocketAddress(ip, port), 5000);
        print = new PrintWriter(socket.getOutputStream());
        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //start connection with server
        String line = reader.readLine();
        System.out.println(line);
    } catch (IOException e) 
    {
        e.printStackTrace();
    }
}

    //quick method to send message
public void sendMessage(String text)
{
    print.println(text);
    print.flush();
    try 
    {
        String line = reader.readLine();
        System.out.println(line);
    } catch (IOException e) 
    {
        e.printStackTrace();
    }
}
}

public static void main(String args[])
{
    client.sendMessage("test");
    client.sendMessage("test2");
    client.sendMessage("test3")
}

    //Server


public static int port = 9884;
public static boolean running = true;

public static ServerSocket serverSocket;
public static Socket socket;
public static PrintWriter writer;
public static BufferedReader reader;

public static void run()
{
    try 
    {
        serverSocket = new ServerSocket(port);
        socket = serverSocket.accept();
        writer = new PrintWriter(socket.getOutputStream(), true);
        writer.println("connection");

        while(running)
        {
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line = reader.readLine();

            if(line != null)
            {
                System.out.println(line);
                writer.println(line);
                writer.flush();
            }
        }
    } catch (IOException e) 
    {
        e.printStackTrace();
    }
}

public static void main(String args[])
{
    run();
}
于 2012-09-25T22:45:00.107 回答
0

请保持简单,

尝试使用InputStream, InputStreamReader, BufferedReader, OutputStream, PrintWriter.

客户端:

Socket s = new Socket();
s.connect(new InetSocketAddress("Server_IP",Port_no),TimeOut); 
// Let Timeout be 5000

服务器端:

ServerSocket ss = new ServerSocket(Port_no);
Socket incoming = ss.accept();

从套接字读取:

InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);
boolean isDone = false;

String s = new String();

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

     System.out.println(s);   // Printing on Console

 }

对于写入套接字:

OutputStream os = s.getOuptStream();
PrintWriter pw = new PrintWriter(os)

pw.println("Hello");
于 2012-09-24T16:15:08.743 回答
0

确保刷新服务器的输出:

stream.flush();
于 2012-09-24T16:17:54.027 回答