Jump to content
  • Advertisement
Sign in to follow this  
MattS423

[java] TCP Sockets and Data Streams

This topic is 4892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey. I created a server-client pair that connect to each other through a TCP socket and they're supposed to be able to talk to each other....only they can't. The server program just stops and does nothing whenever the server attempts to read from the socket data stream. Here's the relevent code:

 PrintWriter out = new PrintWriter(ClientSocket.getOutputStream(), true);
            String InData = "InDataInitString";
            out.println("Handshake"); // <---- This goes through to client just fine
            
           
            BufferedReader in = new BufferedReader(
                        new InputStreamReader(ClientSocket.getInputStream()));
                    
            try
            {
            InData = in.readLine(); // Error occurs here
            System.out.println("Check");
            System.out.println(InData);
            }
            catch(IllegalBlockingModeException e)
            {
                out.println("end");
                System.err.println(e.getMessage());
                System.exit(1);
            }

The catch statements are never reached. the
 in.readLine() 
call just seems to take forever. its obvious that the sockets are connected correctly, because i can send data from server to client just fine. The client is deffinately sending data to the server...the server is just not reading it. Any ideas? I'm kinda new at Java AND new at network programming, so any help would be great. Thanks! Matt

Share this post


Link to post
Share on other sites
Advertisement
The client and server send data the same way, though a PrintWriter. I don;t know if this sends the newline character or not, but to make sure, I sent one explicitly and the results were the same. Intrestingly enough, the client CAN get the text from the server but the server can't get the data from the client, even though they are sent in the same way.

Here's the client code:


public Client() {

Socket kkSocket = null;
BufferedReader in = null;
PrintWriter out = null;
try{
kkSocket = new Socket("DB62T411",4444);
in = new BufferedReader( new InputStreamReader( kkSocket.getInputStream()) );
out = new PrintWriter(kkSocket.getOutputStream());
}
catch(UnknownHostException e)
{
System.err.println(e.getMessage());
System.exit(-1);
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(-2);

}


try{

String InLine = " ";

System.out.println("Begin Read\n");
out.println("Begin Read\r\n"); //this line is not received by server
while((InLine = in.readLine()) != null) //works.
{
System.out.println(InLine);
out.println(InLine);
if(InLine == "end")
{
break;
}


}//end while.

System.out.println("Disconnect");

out.println("end");
out.close();


in.close();
kkSocket.close();
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(-3);
}



}



any other ideas?

Share this post


Link to post
Share on other sites
How's the server code? Is it "looping" properly, I mean, aren't you running on deadlocks? You're using a multithreaded approach, right?

Share this post


Link to post
Share on other sites
here's the server code, with some comments:



PrintWriter out = new PrintWriter(ClientSocket.getOutputStream(), true);
String InData = "InDataInitString";
out.println("Handshake\n"); //client receives this line.


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



InData = in.readLine(); //Hangs here. It MUST be blocking somehow.
//no error, but it just sits.
System.out.println("Check"); //line never reached.
System.out.println(InData);





System.out.println("Starting Loop");
out.println("Starting Loop");
//the Loop
while( (InData = in.readLine()) != null) //same error would occur here if it ever got there.
{

System.out.println("Looped and data present"); //to ensure proper looping. Never reached.
//process InData
//Write the line
System.out.println("Received: " + InData );
out.println("Received: " + InData);

if(InData.equalsIgnoreCase("end"))
{
break;
}



}

System.out.println("Stream Closing");
out.close();
in.close();






As you can see, the in.readLine() call makes it sit and spin.

Share this post


Link to post
Share on other sites
Oh, i thought i had the entire client up there


public Client() {

Socket kkSocket = null;
BufferedReader in = null;
PrintWriter out = null;
try{
kkSocket = new Socket("DB62T411",4444);
in = new BufferedReader( new InputStreamReader( kkSocket.getInputStream()) );
out = new PrintWriter(kkSocket.getOutputStream());
}
catch(UnknownHostException e)
{
System.err.println(e.getMessage());
System.exit(-1);
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(-2);

}


try{

String InLine = " ";

System.out.println("Begin Read\n");
out.println("Begin Read\n");
while((InLine = in.readLine()) != null)
{
System.out.println(InLine);
out.println(InLine + "\n");
if(InLine == "end")
{
break;
}


}

System.out.println("Disconnect");

out.println("end\n");
out.close();



in.close();
kkSocket.close();
}
catch(IOException e)
{
System.err.println(e.getMessage());
System.exit(-3);
}



}



Any ideas? I'm getting despeate here...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!