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!");
    }
}

したがって、私の問題は、ループを取り除き、文字列を2回送信しようとしても送信されないことです。クライアント側で新しいソケットを閉じて作成しない限り、一度だけ実行されます。だから、誰かが私が間違っていることを説明してくれたら、それは素晴らしいことです。どうもありがとう。

4

4 に答える 4

1

ループ内でアウトストリームを開いているのはなぜですか? stream = new PrintStream(socket.getOutputStream());

このステートメントをループの外に出し、ループstream内に書き込みます。

于 2012-09-24T16:18:23.623 に答える
0

サーバーからの出力を必ずフラッシュしてください。

stream.flush();
于 2012-09-24T16:17:54.027 に答える
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

答えてくれたみんなに感謝しますが、それがずっと何だったのかわかりません。私はいくつかの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 に答える