Server -- Client

Hi friends, i'm tring to create a client/server program....this is the code:

Server

class Server{

publicstaticvoid main(String args[]){

String data ="Good Morning";

try{

ServerSocket srvr =new ServerSocket(1234);

Socket skt = srvr.accept();

System.out.print("Server has connected!\n");

PrintWriter out =new PrintWriter(skt.getOutputStream(),true);

System.out.print("Sending string: '" + data +"'\n");

out.print(data);

out.close();

BufferedReader in =new BufferedReader(new

InputStreamReader(skt.getInputStream()));

System.out.print("Received string: '");

while (!in.ready()){}

System.out.println(in.readLine()+"'");// Read one line and output it

in.close();

skt.close();

srvr.close();

}

catch(Exception e){

System.out.print("Server error!\n");

}

}

}

Client

class Client{

publicstaticvoid main(String args[]){

try{

String back ="Also to you!!";

Socket skt =new Socket("localhost", 1234);

BufferedReader in =new BufferedReader(new

InputStreamReader(skt.getInputStream()));

System.out.print("Received string: '");

while (!in.ready()){}

System.out.println(in.readLine()+"'");// Read one line and output it

in.close();

System.out.println("End recieve -- Start send");

PrintWriter out =new PrintWriter(skt.getOutputStream(),true);

System.out.print("Sending string: '" + back +"'\n");

out.print(back);

out.close();

}

catch(Exception e){

System.out.print("Client error!\n");

}

}

}

The problem is that the server send the first string but the client cannot send the second string....i don't know why!!

This is the output of Server:

Server has connected!

Sending string: 'Good Morning'

Server error!

and of the Client:

Received string: 'Good Morning'

End recieve -- Start send

Client error!

Thanks for the assistances..

[3905 byte] By [marlborinoa] at [2007-11-26 18:40:44]
# 1
Implement the sending and receiving in separate threads and not in the same thread.
qUesT_foR_knOwLeDgea at 2007-7-9 6:14:45 > top of Java-index,Java Essentials,Java Programming...
# 2
The thing is that you are coming out of the loop.in.ready when this is true !in.ready will be false and you will never read.
qUesT_foR_knOwLeDgea at 2007-7-9 6:14:46 > top of Java-index,Java Essentials,Java Programming...
# 3
And you are trying to read when the stream is not ready! A lot of logical mistakes in the code.
qUesT_foR_knOwLeDgea at 2007-7-9 6:14:46 > top of Java-index,Java Essentials,Java Programming...
# 4

Take a look at these two codes.

One is server and other client.I wrote it a long time back when i was still learning the basics in java

kserver.java

import java.io.*;

import java.net.*;

class kserverreceive implements Runnable

{

Socket client1;

BufferedReader in;

String fromuser;

Thread t;

kserverreceive(Socket client1,BufferedReader in)

{

this.client1=client1;

this.in=in;

t=new Thread(this);

t.start();

}

public void run()

{

try

{

while((fromuser=in.readLine())!=null)

{

System.out.println(fromuser);

}

}

catch(IOException e)

{

System.out.println("IOException encountered");

}

}

//System.out.println("client has disconnected");

}

class kserversend implements Runnable

{

Socket client2;

BufferedReader stdIN=new BufferedReader(new InputStreamReader(System.in));

PrintWriter out;

String touser;

Thread t;

kserversend(Socket client2,PrintWriter out)

{

this.client2=client2;

this.out=out;

t=new Thread(this);

t.start();

}

public void run()

{

try

{

while((touser=stdIN.readLine())!=null)

{

out.println(touser);

}

}

catch(IOException e)

{

System.out.println("IOException encountered");

}

}

//System.out.println("the client has disconneceted");

}

public class kserver

{

public static void main(String args[]) throws IOException

{

ServerSocket server=null;

Socket client=null;

BufferedReader in;

PrintWriter out;

kserverreceive client1;

kserversend client2;

try

{

server=new ServerSocket(7777);

}

catch(IOException e)

{

System.out.println("couldnt listen on port 7777");

System.exit(1);

}

try

{

while(true)

{

client=server.accept();

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

out=new PrintWriter(client.getOutputStream(),true);

System.out.println("connection established");

client2=new kserversend(client,out);

client1=new kserverreceive(client,in);

}

}

catch(Exception e)

{

System.out.println("accept failed:"+e);

System.exit(1);

}

//This is if you want to accept only a single client request

/*String outline;

BufferedReader inread=new BufferedReader(new InputStreamReader(client.getInputStream()));

while((outline=inread.readLine())!=null)

{

System.out.println(outline);

}*/

//inread.close();

client.close();

server.close();

}

}

and

kclient.java

import java.io.*;

import java.net.*;

class kclientsend implements Runnable

{

Socket client1;

PrintWriter out;

Thread t;

String touser;

BufferedReader stdIN=new BufferedReader(new InputStreamReader(System.in));

kclientsend(Socket client1,PrintWriter out)

{

this.client1=client1;

this.out=out;

t=new Thread(this);

t.start();

}

public void run()

{

try

{

while((touser=stdIN.readLine())!=null)

{

out.println(touser);

}

}

catch(IOException e)

{

System.out.println("IOException encountered");

}

}

//System.out.println("The client has disconnected");

}

class kclientreceive implements Runnable

{

Socket client2;

BufferedReader in;

String fromuser;

Thread t;

kclientreceive(Socket client2,BufferedReader in)

{

this.in=in;

this.client2=client2;

t=new Thread(this);

t.start();

}

public void run()

{

try

{

while((fromuser=in.readLine())!=null)

{

System.out.println("["+client2.getInetAddress().toString()+"]::"+client2.getInetAddress().getHostName()+"::"+fromuser);

}

}

catch(IOException e)

{

System.out.println("IOException encountered");

}

}

//System.out.println("The client has disconnected");

}

public class kclient

{

public static void main(String args[]) throws Exception

{

Socket client=null;

PrintWriter out=null;

BufferedReader in=null;

kclientsend client1;

kclientreceive client2=null;

try

{

client=new Socket("NAMI",7777);

out=new PrintWriter(client.getOutputStream(),true);

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

System.out.println("connection established successfully");

client1=new kclientsend(client,out);

client2=new kclientreceive(client,in);

}

catch(UnknownHostException e)

{

System.out.println("NAMI not found"+e);

System.exit(1);

}

catch(IOException e)

{

System.out.println("couldnt get IO for NAMI"+e);

System.exit(1);

}

/*String fromuser;

BufferedReader stdIN=new BufferedReader(new InputStreamReader(System.in));

while((fromuser=stdIN.readLine())!=null)

{

out.println(fromuser);

}*/

Thread.sleep(100000);

out.close();

// stdIN.close();

client.close();

}

}

Run the server first and then the client. Change the name of the host you are trying to connect and see.

qUesT_foR_knOwLeDgea at 2007-7-9 6:14:46 > top of Java-index,Java Essentials,Java Programming...
# 5
> class kserverreceiveI hope that you now know how to name classes, and that classes should start with upper case character.KajPs. ..and don't start threads in constructors (especially not if the class is implementing Runnable)
kajbja at 2007-7-9 6:14:46 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks...this code is a very good assistance for me...now i've understood my problem!!
marlborinoa at 2007-7-9 6:14:46 > top of Java-index,Java Essentials,Java Programming...
# 7

> > class kserverreceive

>

> I hope that you now know how to name classes, and

> that classes should start with upper case character.

>

> Kaj

>

> Ps. ..and don't start threads in constructors

> (especially not if the class is implementing Runnable)

Yes i know that. As i mentioned i wrote that code when i was still in the very very basics of java around 3 months back somewhere in november(started with java on october 20th) and you can see i never knew anything about indentation and coding conventions(was pathetic) from my code. It was then i learnt about coding conventions and using meaningful variable and method names.

qUesT_foR_knOwLeDgea at 2007-7-9 6:14:46 > top of Java-index,Java Essentials,Java Programming...