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..
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.