I am creating two sockets: a client socket and a server socket.
ServerSock class:
public class ServerSock {
public static void main(String args[]) throws Exception{
//creating welcome socket @param: port number
System.out.println("Server is started...");
ServerSocket serverSocket = new ServerSocket(55555);
System.out.println("Server is wating for client request...");
//creating individual sockets for clients
Socket socket0 = serverSocket.accept();
System.out.println("Client connected...");
//reading the input data
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket0.getInputStream()));
//reading the data from bufferedReader
String str = bufferedReader.readLine();
//console the data
System.out.println(" client data : " + str);
}
}
ClientSocket class:
public class ClientSocket {
public static void main(String args[]) throws Exception{
//ip address of the server
String ip = "localhost";
//port number of the application
int port = 55555;
//creating a socket
Socket socket = new Socket(ip, port);
//data to send
String data0 = "lakshan waruna";
//converting data to ByteStream param: where to send the data
OutputStreamWriter os = new OutputStreamWriter(socket.getOutputStream());
//writing the data
os.write(data0);
os.flush();
}
}
ServerSock
runs until I start the client socket. It also prints the "client connected" statement in the ServerSock
class. But when it tries to read data from bufferedReader using the statement String str = bufferedReader.readLine();
, it throws a SocketException
stating "connection reset". How can I fix this issue?