• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help me in this code!

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
}
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic