I created 2 files Client.java and Server.java and i tried to send the data using sockets to Server side and display it.
But I am getting a Run-time exception.
Please help
The code along with the output(within comments) is displayed below.
Client.java:-
==============================
import java.net.*;
import java.io.*;
class Client
{
public static void main(
String args[])
{
Socket s=null;
PrintWriter out=null;
try{
s=new Socket("127.0.0.1",8088);
}
catch(UnknownHostException e)
{
System.out.println("E1:"+e);
}
catch(IOException e)
{
System.out.println("E2:"+e);
}
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter data:");
try{
String str=br.readLine();
out =new PrintWriter(s.getOutputStream());
out.println(str);
}
catch(IOException e)
{System.out.println("E3:"+e);}
}
}
/*Output:-
C:\Tarun\java\Network>
java Client
Enter data:
hello
*/
Server.java:-
===============================================
import java.net.*;
import java.io.*;
class Server
{
public static void main(String args[])
{
ServerSocket s=null;
Socket clientconn=null;
BufferedReader br=null;
String s1="";
try{
s=new ServerSocket(8088);
clientconn=s.accept();
System.out.println(clientconn.getInetAddress());
System.out.println(clientconn.getLocalPort());
}
catch(IOException e)
{
System.out.println("E1:"+e);
}
try{
String str="";
br=new BufferedReader(new InputStreamReader(clientconn.getInputStream()));
System,.out.println(br);
s1=br.read();
System.out.println(br.readLine());
}
catch(IOException e)
{
System.out.println("E2:"+e+" String s1:"+s1);
}
}
}
/*Output:-
C:\Tarun\java\Network>java Server
/127.0.0.1
8088
E2:java.net.SocketException: Connection reset String s1:
*/