Solved

Sockets

Posted on 2009-07-12
10
202 Views
Last Modified: 2012-05-07
I have written a simple client server program using sockets. There is no compilation and run time error. However the application does not seem to do the logics I have written. Did I miss something?.
Client

-------
 

import java.io.*;

import java.net.*;

 

public class Client

{

	Socket s;

	PrintWriter out;

	BufferedReader in;

	

	public Client()

	{

		try

		{ 

	        s = new Socket("127.0.0.1",1418);

	        out = new PrintWriter(s.getOutputStream());

	        in = new BufferedReader(new InputStreamReader(s.getInputStream()));

	        out.write("Hi");

	        String m = in.readLine();

	        System.out.println(m);

		}

		catch(Exception e)

		{

			System.out.println(e);

		}

	}

	 

    public static void main(String[] args) 

    {

        Client m = new Client();

    }

}
 
 

Server

---------
 

public class Server 

{

	PrintWriter out;

	BufferedReader in;

	ServerSocket ss;

	Socket sc;

	

	public Server()

	{

		try

		{

			ss = new ServerSocket(1418);

			sc = ss.accept();

			out = new PrintWriter(sc.getOutputStream());

			in = new BufferedReader(new InputStreamReader(sc.getInputStream()));

			String m = in.readLine();

			out.write(m);

		}

		catch(Exception e)

		{

			System.out.println(e);

		}

	}

	

    public static void main(String[] args) 

    {

    	Server s = new Server();

    }

}

Open in new window

0
Comment
Question by:eugene007
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Assisted Solution

by:ozlevanon
ozlevanon earned 20 total points
Comment Utility
Your client is writing just 2 characters "hi", but the server is inovking readLine, which waits for a newline character before it returns.
If you change you client to write "hi\n" it will probably fix it. Also, pay attention that calling write() method doesn't guarantee that the data will be flushed, so in any event, call flush() when you want to make sure the data is actually written.
0
 

Author Comment

by:eugene007
Comment Utility
I tried and it does not work.
0
 

Author Comment

by:eugene007
Comment Utility
Also I switched write() method to println() method.
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 20 total points
Comment Utility
Try flush after you write to PrintWriter both on the client
and the server side.
0
 

Author Comment

by:eugene007
Comment Utility
Now it works. This is quite odd. I need to flush the data even when im using the println() method to write.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
Comment Utility
>>I need to flush the data even when im using the println() method to write.

You won't do if you do

out = new PrintWriter(s.getOutputStream(), true);
0
 

Author Comment

by:eugene007
Comment Utility
so the purpose of the flush() method is to ensure that what is written are commited.
0
 
LVL 23

Expert Comment

by:Ajay-Singh
Comment Utility
Yes.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>so the purpose of the flush() method is to ensure that what is written are commited.

No. It's in no way transactional. There's no 'rollback' for instance. And when you close(), it will get flushed automatically.

Its purpose is to transfer what's held in the buffers to the stream immediately
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countPairs challenge 7 57
topping2 challenge 13 55
create a gui in perl 3 42
unix in java example 9 37
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now