aspose file tools*
The moose likes Java in General and the fly likes Problem in Networking concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem in Networking concept" Watch "Problem in Networking concept" New topic
Author

Problem in Networking concept

Narayanan Ramaswamy
Greenhorn

Joined: Jan 23, 2006
Posts: 2
Hi All,

I am trying to write an Echo program in Java.
I can connect the server from the client. but the data was not transferred.

My Code is

Server:

import java.net.*;
import java.io.*;
public class Server
{
public static void main(String args[])
{
String data;
try
{
ServerSocket ss=new ServerSocket(21);
Socket s=ss.accept();
DataInputStream in=new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
//System.out.println("test0");
while(true)
{
System.out.println("test1");
data=in.readLine();
out.writeBytes(data);
System.out.println(data);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Client:

import java.net.*;
import java.io.*;
public class Client
{
public static void main(String args[])
{
String data;
try
{
Socket s=new Socket("127.0.0.1",21);
DataInputStream in=new DataInputStream(s.getInputStream());
DataOutputStream out=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(System.in);
data=dis.readLine();
while(data!="quit")
{
System.out.println("client:"+data);
out.writeBytes(data);
System.out.println("server:"+in.readLine());
data=dis.readLine();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Please try to find out the problem...

thanks and Regards,
Narayanan
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

One problem I can see right away is that both ends of the connection are reading using readLine() (which reads until it sees a newline, but discards the newline) and writing the String using writeBytes() (and not sending a newline). So both ends of the connection send insufficient data for the other end to read.


[Jess in Action][AskingGoodQuestions]
Narayanan Ramaswamy
Greenhorn

Joined: Jan 23, 2006
Posts: 2
Ok.
But what function i can use to fulfill the requirement.
please help....
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

write((byte) '\n') ?
 
wood burning stoves
 
subject: Problem in Networking concept