Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-26-2007, 08:28 AM
Member
 
Join Date: Dec 2007
Posts: 19
Rep Power: 0
JavaEmpires is on a distinguished road
Default [B]Simple Client connected to server but not exchanging messages[/B]
Hello all,
I m new to Networking.
I m trying to write a simple client-server program wherein server greets client when connected and vice versa.

Here's is a complete Client and Server program.

/*Server.java*/

public class Server extends Thread {
public static final int V_PORT = 2013;
protected ServerSocket server_socket;

public Server()
{
try{
InetAddress iaddr = InetAddress.getLocalHost();
server_socket = new ServerSocket(V_PORT, 5, iaddr);
System.out.println("\nConnected to InetAddress " + server_socket.getInetAddress().toString());

System.out.println("\nPort " + V_PORT+" is up and running");

}


catch(Exception e){
System.out.println("\nError : Port " + V_PORT+" is already used by another process");
}
}



public void run(){
InputStream receive;
OutputStream send;
String msg = "Hello Client WelCome!";
String output="";
byte b[] = new byte[100];
while(true){
try{
Socket client = server_socket.accept();
send = client.getOutputStream();
send.write(msg.getBytes());
receive = client.getInputStream();
receive.read(b);
System.out.println("\nClient Said : "+ b.toString());



}
catch(Exception e){
System.out.println("\nCould not connect to client" + e.toString());
e.printStackTrace();

}
}

}

public static void main(String args[]){
new Server().start();
}
}

/*Client.java*/

public class Client {

public static final int V_PORT = 2013;
public Socket client_socket = null;
InputStream receive = null;
OutputStream send = null;
String msg="Hello Server";

public Client(){
byte b[] = new byte[100];
try{
InetAddress addr = InetAddress.getLocalHost();
client_socket = new Socket(addr, V_PORT);
System.out.println("\nConnected to port " + V_PORT);
System.out.println("\nConnected to InetAddress " + client_socket.getInetAddress().toString());
receive = client_socket.getInputStream();
receive.read(b);
System.out.println("\nString read from Server = "+b.toString());

send = client_socket.getOutputStream();
send.write(msg.getBytes());
System.out.println("\nMessage Written to the server");


}


catch(Exception e){
System.out.println("\nClient Error : "+e.toString());
}
}


public static void main(String args[]){
new Client();
}
}

Please help me to resolve this problem.

Thanx in advance.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-06-2008, 07:58 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 838
Rep Power: 4
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Default
I'm not sure I see your problem. See my screenshot to see that I was able to send a message to the server from the client, also upon connect a message was sent from the server.



Appears to work.. no? just not sure what it is you're requiring it to do exactly.
__________________
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-06-2008, 07:52 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
Rep Power: 0
jelly is on a distinguished road
Default
The problem you are seeing comes from the line:

System.out.println("\nString read from Server = "+b.toString());

This line is calling toString() on the byte array - which by default is simply printing the memory address. I suspect what you are trying to do is print the contents of the byte array, you need to create a string from the bytes:

System.out.println("\nString read from Server = "+ new String(b));

Hope that helps
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-07-2008, 08:01 AM
Member
 
Join Date: Dec 2007
Posts: 19
Rep Power: 0
JavaEmpires is on a distinguished road
Default
Thank you all.
That solved my problem.
Thank you very much
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Simple Web Server Java Tip java.net 0 04-07-2008 09:04 PM
client/server messages exchange after 5 min dim_ath Advanced Java 2 01-22-2008 09:46 AM
how can server send messages every 5 min? dim_ath Networking 7 01-10-2008 04:59 PM
Identify Client in Socket Client Server Application masadjie Networking 1 12-20-2007 10:18 AM
Simple example Client Server Application ferosh Networking 1 04-01-2007 11:36 AM


All times are GMT +2. The time now is 10:29 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org