Results 1 to 15 of 15
- 07-12-2011, 06:06 PM #1
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
Really big problem in client and server program
Friends here is my client program in java using sockets
Here client sends seconds to server by using thread and incrementing a variable for every 1 second.Java Code:import java.io.*; import java.net.*; class Client { int count=0; public static void main(String args[]) throws Exception { int count=0; Socket s=new Socket("LocalHost",2000); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); String str=new String(); while(true) { try { Thread.sleep(1000); count++; str=""+count+""; dos.writeBytes("str"); System.out.println(str); } catch(Exception e) {} } } }
And server program is
THERE IS NO ERRORS IN ABOVE CODEJava Code:import java.io.*; import java.net.*; class Server { public static void main(String args[]) throws Exception { ServerSocket ss=new ServerSocket(2000); Socket s=ss.accept(); DataInputStream dis=new DataInputStream(s.getInputStream()); String str=dis.readLine(); System.out.println("While"); while(true) { System.out.println(str); str=dis.readLine(); } } }
Now the big problem is server is waiting for message from client , but client is posting the values to the server program.
When i execute the program by executing server first next to client(which is to be) client is sending data (which i can see the values the client is printing in cmd) but server program just stays in the line "String str=dis.readLine();" and just waiting for input...
So please help me in getting this code..
Thanks.....Last edited by helioshilary; 07-12-2011 at 06:50 PM.
- 07-12-2011, 06:09 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,145
- Rep Power
- 5
It amazes me that people can figure out how to post multiple unnecessary animated icons which do nothing to help explain the problem, but they can't figure out how to post code so that is retains its formatting and is actually readable.
- 07-12-2011, 06:15 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
@OP: Use code tags
[code]
YOUR CODE HERE
[/code]
And ask specific questions. If you have errors, post them as well(in code tags).
- 07-12-2011, 06:30 PM #4
This is confusing. How is the server getting data from the server?server is not getting data from server
Please copy and paste the output from your program that traces what it is doing.
Add comments to the output that describes what is wrong and what you want it to do differently.
- 07-12-2011, 06:54 PM #5
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
in the image

the main problem is the server command prompt must also get the values as it is in client command prompt,,,,,but server program is not getting any value and it is not displaying any..
- 07-12-2011, 07:10 PM #6
Your write methods do not match your input methods.
The you are writing bytes and trying to read a String with a lineend.
Change the methods so they match.
- 07-12-2011, 07:12 PM #7
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
in which code is that in server or client one?
- 07-12-2011, 07:14 PM #8
There is code in both the server and in the client.
- 07-12-2011, 07:19 PM #9
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
Then what method i have to use in client code and what method i have to read in server code..
- 07-12-2011, 07:43 PM #10
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
the same methods even worked for me when i try to send some string from client to server by reading from user...........
- 07-12-2011, 08:00 PM #11
how is the code different when it worked than in the code you posted here?the same methods even worked for me
- 07-12-2011, 11:55 PM #12
You probably need to flush() your output stream.
Get in the habit of using standard Java naming conventions!
- 07-13-2011, 02:53 AM #13
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
The code is simple
Java Code:Server code import java.io.*; import java.net.*; class Server { public static void main(String args[]) throws Exception { System.out.println("Waiting for client message....."); ServerSocket ss=new ServerSocket(2000); Socket s=ss.accept(); DataInputStream dis=new DataInputStream(s.getInputStream()); String str=dis.readLine(); while(!(str.equals("123"))) { System.out.println("Client :"+str); str=dis.readLine(); } } }This code is working all i need to be executed the previous code using threads,,,please help me.....Java Code:Client code import java.io.*; import java.net.*; class Client { public static void main(String args[]) throws Exception { Socket s=new Socket("LocalHost",2000); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); DataInputStream dis=new DataInputStream(System.in); System.out.println("Enter msg:"); String str=dis.readLine(); while(true) { if(str.equals("123")) { dos.writeBytes(str); System.exit(0); } else dos.writeBytes(str+"\n"); System.out.println("Enter msg:"); str=dis.readLine(); } //s.close(); } }
- 07-13-2011, 03:04 AM #14
Do you really write your code with no indentations for nesting/scope levels?
It makes it very hard to read if the nested code is not indented. Especially to find matching { and }
Following from the Client code in your post #13
dos.writeBytes(str+"\n"); // Notice the \n here
See the second line in post #6Last edited by Norm; 07-13-2011 at 03:26 AM.
- 07-13-2011, 01:17 PM #15
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Simple Client (2) - Server program
By Reztem in forum New To JavaReplies: 2Last Post: 01-09-2012, 02:05 AM -
how to run server-client program in desktop application??
By miu44 in forum NetBeansReplies: 1Last Post: 02-16-2011, 10:05 AM -
Simple Socket program: Java Client- C server
By pimmling in forum New To JavaReplies: 1Last Post: 11-08-2010, 01:27 PM -
Client Server program, a tiny problem.
By skarosg3 in forum NetworkingReplies: 12Last Post: 05-27-2010, 01:03 PM -
Multithreaded Client/Server Chat program
By f0ns in forum Threads and SynchronizationReplies: 3Last Post: 10-21-2009, 05:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks