Results 1 to 4 of 4
- 12-26-2007, 07:28 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 19
- Rep Power
- 0
[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.
- 01-06-2008, 06:58 AM #2
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)
- 01-06-2008, 06:52 PM #3
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
- 01-07-2008, 07:01 AM #4
Member
- Join Date
- Dec 2007
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
A Simple Web Server
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:04 PM -
client/server messages exchange after 5 min
By dim_ath in forum Advanced JavaReplies: 2Last Post: 01-22-2008, 08:46 AM -
how can server send messages every 5 min?
By dim_ath in forum NetworkingReplies: 7Last Post: 01-10-2008, 03:59 PM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM -
Simple example Client Server Application
By ferosh in forum NetworkingReplies: 1Last Post: 04-01-2007, 10:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks