MyServer.java
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(5001);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
if ("Hi".equals(str)){
dout.writeUTF("How are you?");
} else if ("Bye".equals(str)){
dout.writeUTF("Thankyou! Have a Good day!");
} **else if (str != null)){
try {
String numbers;
numbers = str.replaceAll("[^0-9]", "");
int number = Integer.parseInt(numbers);
dout.writeUTF("The line is being printed");
for (int i = 0; i < number; i++) {
dout.writeUTF(str.replaceAll("[^a-z,^A-Z]", ""));
}
} catch (Exception e) {
//TODO: handle exception
}**
} else {
dout.writeUTF("Sorry!");
}
dout.flush();
dout.close();
s.close();
ss.close();
} catch(Exception e) {
System.out.println(e);
}
}
}
MyClient.java
import java.io.*;
import java.net.*;
import java.util.*;
public class MyClient {
public static void main(String[] args) {
try{
Scanner sc = new Scanner(System.in);
Socket s=new Socket("localhost",5001);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
String str1= sc.nextLine();
dout.writeUTF(str1);
dout.flush();
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
dout.close();
dis.close();
s.close();
} catch(Exception e) {
System.out.println(e);}
}
}
I am giving input to the server from the client-side and want that input to be printed given a number of times on the client-side. But not able to do that. Can anyone let me know what mistake I am making here? It is replying to message "Hi" and "Bye", everything else is working fine.
The following is your code with my corrections.
(Notes after the code.)
Class MyServer
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) {
try (ServerSocket ss = new ServerSocket(5001)) {
Socket s = ss.accept();// establishes connection
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
String str = dis.readUTF();
System.out.println("message= " + str);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
if ("Hi".equals(str.trim())) {
dout.writeUTF("How are you?");
}
else if ("Bye".equals(str)) {
dout.writeUTF("Thankyou! Have a Good day!");
}
else if (str != null) {
try {
String numbers;
numbers = str.replaceAll("[^0-9]", "");
int number = Integer.parseInt(numbers);
dout.writeUTF("The line is being printed");
for (int i = 0; i < number; i++) {
dout.writeUTF(str.replaceAll("[^a-z,^A-Z]", ""));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
dout.writeUTF("END");
dout.flush();
}
catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(2000L);
}
catch (InterruptedException xInterrupted) {
// Ignore.
}
}
}
Class MyClient
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.Socket;
import java.util.Scanner;
public class MyClient {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
try (Socket s = new Socket("localhost", 5001)) {
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF(str1);
dout.flush();
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
String str = dis.readUTF();
while (!"END".equals(str)) {
System.out.println("message= " + str);
str = dis.readUTF();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Sending and receiving data via sockets is not instantaneous. Method readUTF
will wait until there is data to read. It will read all the data sent in the last invocation of method writeUTF
. Method readUTF
will wait indefinitely. Hence the server needs to send notification to the client that there is no more data to send. In the above code I send the string END to indicate the end of the data that the server is sending. Note also that you only need to close resources that you explicitly create. In class MyServer
, this means ServerSocket
only. I use try-with-resources to ensure that ServerSocket
is closed.
Similarly, in class MyClient
, the only resource that needs to be explicitly closed is the Socket
– which I again do using try-with-resources.
If the server terminates, the socket is closed. Hence, in class MyServer
, after sending data to the client, the server waits for two seconds which is hopefully enough time for the client to read that data.