Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-26-2007, 08:28 AM
Member
 
Join Date: Dec 2007
Posts: 19
JavaEmpires is on a distinguished road
[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
Sponsored Links
  #2 (permalink)  
Old 01-06-2008, 07:58 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 740
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
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
jelly is on a distinguished road
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
JavaEmpires is on a distinguished road
Thank you all.
That solved my problem.
Thank you very much
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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 +3. The time now is 02:25 PM.


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