0

您好我正在尝试通过套接字编程创建聊天应用程序。

但是,我无法在客户端或服务器端发送或接收消息。只有 System.out.println 语句显示为输出。我正在放置相同的代码。

服务器:

  import java.net.*;
    import java.io.*;


class MyServer
{
  public static void main(String arr[])
    {
    try

        {
        ServerSocket server=new ServerSocket(2500);//the server is listening to at this port
        System.out.println("Server is ready , waiting for a connection request...");



/*Now the accept() method blocks while it is waiting for a client Socket connection.When a client finally tries to connect, the method returns a plain old Socket (on a 

diffrent port) that knows how to communicate with the client(i.e. knwos the client IP address and port number).The SOcket is on a different port than the ServerSocket, 

so that the ServerSocket can go back to waiting for other clients.*/



        Socket sock=server.accept();
        System.out.println("Request received , connection completed...");
        Thread.sleep(10000);
        System.out.println("Waiitng for message...");
        BufferedReader b=new BufferedReader(new InputStreamReader(sock.getInputStream()));
        String msg=b.readLine();
        System.out.println("Received message "+msg);
        Thread.sleep(10000);

        System.out.println("Sending acknowledgement");
        PrintStream out=new PrintStream(sock.getOutputStream());
        Thread.sleep(30000);
        out.println("Hello client, your test message is received");
        System.out.println("Ack sent, clsoing connection");
        Thread.sleep(50000);
        server.close();
        sock.close();

        System.out.println("Connection closed");
        }
        catch(Exception ex)
        {

        System.out.println(ex);
        }

    }

}

客户端:

  import java.net.*;
    import java.io.*;


   class MyClient
    {
  public static void main(String arr[])
    {
    try

        {

        System.out.println("Client ready , sending request...");
        Thread.sleep(10000);
        Socket socket=new Socket("localhost",2500);;    
        Thread.sleep(10000);
        System.out.println("Connection completed , sending message");
        PrintStream out=new PrintStream(socket.getOutputStream());
        Thread.sleep(30000);
        out.println("Hello server, its a test message");

        BufferedReader b=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String msg=b.readLine();
        System.out.println("Ack message "+msg);
        Thread.sleep(10000);


        System.out.println("clsoing connection");
        Thread.sleep(50000);
        socket.close();

        System.out.println("Connection closed");
        }
        catch(Exception ex)
        {

        System.out.println(ex);
        }

    }

}

输出:在客户端:

D:\java>java MyClient
Client ready , sending request...
Connection completed , sending message
Ack message Hello client, your test message
clsoing connection
Connection closed

在服务器上:

D:\java>java MyServer
Server is ready , waiting for a connection request...
Request received , connection completed...
Waiitng for message...
Received message Hello server, its a test message
Sending acknowledgement
Ack sent, clsoing connection
Connection closed

我无法通过键盘输入

4

1 回答 1

0

您需要制作一个扫描仪对象以从客户端的控制台接收输入,然后将其发送到服务器

先加

import java.util.*;

在顶部,然后替换

out.println("Hello serverits a test message");

Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = sc.nextLine();        
out.println(str);

它会提示您在客户端控制台中输入一个字符串..

于 2012-05-12T10:49:30.797 回答