Skip to main content

Socket Programming -Help needed

2 replies [Last post]
rpolic
Offline
Joined: 2013-06-07
Points: 0

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ravi;
import java.io.*;
import java.net.*;

/**
*
* @author ravirocks
*/
public class Client {
public static void main(String args[])throws Exception{
Client c1 = new Client();
c1.run();

}
public void run(){
try{
Socket s1 = new Socket("localhost",9999);
PrintStream p1 = new PrintStream(s1.getOutputStream());
//p1.println("Hello Server");
System.out.println("Enter soemthing");
InputStreamReader i = new InputStreamReader(System.in);
BufferedReader b = new BufferedReader(i);
p1.println();
}

catch (Exception e1)
{}

}

}

------------------------------------------------------------------------------------------------------------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ravi;
import java.io.*;
import java.net.*;
/**
*
* @author ravirocks
*/
public class Server {

public static void main(String aargs[])throws Exception{

Server s1 = new Server();
s1.run();
}
public void run(){
try{
ServerSocket c1 = new ServerSocket(9999);
Socket ss_accept = c1.accept();
BufferedReader b1 = new BufferedReader(new InputStreamReader(ss_accept.getInputStream()));
//PrintWriter p1 = new PrintWriter(c1.getOutputStream(),true);
String temp = b1.readLine();
System.out.println("i have recieved"+temp);
}
catch (Exception e1)
{

//e1.printStackTrace();
}

}

}

Whats wrong with this code? I am not able to run it.
Please do the needful

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nirupama_nl
Offline
Joined: 2013-06-14
Points: 0

In the client program, you've a prompt but no statement to capture the input. Next, you've to write to the stream so that the server can receive and display the transmitted message.
Include the following lines in the client program:

String s=b.readLine();
p1.println(s);

Run the server. Run the client, enter some input and see the input echoed in the server window.

rpolic
Offline
Joined: 2013-06-07
Points: 0

Hi,

Thank you.

Actually it worked.First i ran the server and client as u said.
And yes, i will consider your opinion too.
:-)