jQuery in Action, 2nd edition*
The moose likes Sockets and Internet Protocols and the fly likes Help me in this code! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Help me in this code!" Watch "Help me in this code!" New topic
Author

Help me in this code!

Vu Trang
Greenhorn

Joined: Nov 29, 2006
Posts: 2
I'm a newbie in Java
I wrote a short code to run an application in java.net
I don't understand why client can't receive the result returned from server.
Can you help me?

Server:
import java.net.*;
import java.lang.*;
import java.io.*;

public class ServerReverse
{
public static void main(String[] args)
{

try
{
ServerSocket server = new ServerSocket(2266);

Socket client = server.accept();

System.out.println("The connection starts");

InputStreamReader reader = new InputStreamReader(client.getInputStream());
BufferedReader inStream = new BufferedReader(reader);
String str = inStream.readLine();
System.out.println("Received String from client : " +str);

int len = str.length();
char outChar[] = new char[len];
for (int i=0; i<len; i++)
outChar[len-i-1] = str.charAt(i);
String reversedStr = String.valueOf(outChar);

System.out.println("Reversed String : " +reversedStr);

DataOutputStream sentToClient = new DataOutputStream(client.getOutputStream());
for (int i=0; i<reversedStr.length(); i++)
sentToClient.write((byte)reversedStr.charAt(i));

client.close();
server.close();
}



catch (IOException e)
{
System.out.println("Has error when opening the connection");
}
}
}


Client :
import java.net.*;
import java.io.*;
import java.lang.*;

public class ClientReverse
{
public static void main(String[] args)
{
try
{
Socket client = new Socket("LocalHost",2266);

System.out.print("Nhap chuoi can xu ly gui den Server : ");
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader inStream = new BufferedReader(reader);
String sentStr = inStream.readLine();
System.out.println("Sent String : " +sentStr);

DataOutputStream outStr = new DataOutputStream(client.getOutputStream());
outStr.writeBytes(sentStr);

InputStreamReader reversedStr = new InputStreamReader(client.getInputStream());
BufferedReader stream = new BufferedReader(reversedStr);
String strEnd = stream.readLine();

System.out.println("Reversed String : " +strEnd);

outStr.close();
client.close();
}


catch (IOException e)
{
System.out.println(e);
}
}
}
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8833
    
    7

Welcome to the JavaRanch, Vu.
Your problem is simple: You are using a BufferedReader to read lines but you aren't writing any line termination characters to the socket.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help me in this code!
 
Similar Threads
urgent
i am in trouble
button hang in threaded server
Socket Data Conversion Problem
client-serer...plz help