2012-05-12 14 views
0

こんにちは私はソケットプログラミングを介してチャットアプリケーションを作成しようとしています。コマンドラインのJavaチャットアプリケーションがメッセージを送受信していません

しかし、メッセージを送信したり、クライアントまたはサーバー側で受信できません。 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 

は私が

+0

http://stackoverflow.com/q/1582097/1376167 これはあなたを助ける必要があります...もっとヘルププラザため あなたのコードを投稿してください。 – WickeD

+0

@WickeDRevisitedこの質問は長い間続きました...あなたの答えをありがとうございました。 – Deep

答えて

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); 

それはあなたのクライアントコンソールに文字列を入力するよう求められます。..

関連する問題