I need to know the flow of the below given program:
Client Class:
1. Socket sock = new Socket("localhost", 1101);
2. PrintWriter write = new PrintWriter(sock.getOutputStream());
3. write.println("Hello");
4. write.close();
Server Class:
1. ServerSocket sersock = new ServerSocket(1101);
2. sock = sersock.accept();
3. InputStreamReader read = new InputStreamReader(sock.getInputStream());
4. BufferedReader buf = new BufferedReader(read);
5. System.out.println(buf.readLine());
6. buf.close();
When I run the server class and then the client class, how does the flow of the program work?
It flows exactly how it reads.
Server binds the socket to port 1101, listens for connections
1. ServerSocket sersock = new ServerSocket(1101);
2. sock = sersock.accept();
Client connects to server and sends "hello" and closes connection.
1. Socket sock = new Socket("localhost", 1101);
2. PrintWriter write = new PrintWriter(sock.getOutputStream());
3. write.println("Hello");
4. write.close();
Server reads and prints line from input stream after a connection has been made then closes the reader.
3. InputStreamReader read = new InputStreamReader(sock.getInputStream());
4. BufferedReader buf = new BufferedReader(read);
5. System.out.println(buf.readLine());
6. buf.close();
Related
I have a small TCP server and client where the server is performing inside a while(true) loop meanwhile my client isn't. However when i send a message from the client to the server, the server isn't able to read it. Below is the code and error:
SERVER:
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("[SERVER]: Server launched on port " + port);
while (true) { // the while loop makes the server continuously accept client request
Socket clientSocket = serverSocket.accept();
System.out.println("New client connected: " + clientSocket);
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.println(reader.readLine());
}
CLIENT:
Socket socket = new Socket("localhost", 9101);
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("lol");
ERROR:
Exception in thread "main" java.net.SocketException: Connection reset
The error occurs when we try to read data in the server. With all that being said, if i surround the out.println() method in the client in a while(true) loop the error goes away and the code works fine. So i'm wondering why the i get this exception when the server is inside an infinite loop but the client isn't.
So I am attempting to send data to myself and receive the data then print it, now I have been testing for a while and I have noticed its not sending anything, in fact, maybe it is and I am not receiving it properly, I need assistance with this please.
This is what I am using to send data
String host = "127.0.0.1";
int port = Options.port;
Socket socket = new Socket(host, port);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(msg + "\n");
bw.flush();
This is what I am using to receive, I always use this method and it never works so I would not be surprised if this was the root cause.
ServerSocket serverSocket = new ServerSocket(Options.port);
System.out.println("[Listening on port] " + Options.port);
while(true){
Socket socket = serverSocket.accept();
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println(message);
}
1) replace:
Socket socket = serverSocket.accept();
socket = serverSocket.accept();
with:
Socket socket = serverSocket.accept();
2) If this does not solve the problem, set ports as int values, without Options.port
i'm working on an application to print to my local wifi printers but over 9100 port i have no response, over the 515 i have "connection refused".
the code is this:
Socket test = new Socket();
Socket sock = new Socket(ip,9100);
PrintWriter oStream = new PrintWriter(sock.getOutputStream());
oStream.printf("========================\n");
oStream.printf("PROVA STAMPA\n");
oStream.printf("HELLO SLOW SUD\n");
oStream.printf("========================"+"\n");
oStream.close();
sock.close();
and also i have try this:
Socket objSocket = new Socket();
InetSocketAddress objEndPoint = new InetSocketAddress(ip, 9100);
DataOutputStream objOutputStream;
objSocket.connect(objEndPoint, 6000);
objOutputStream = new DataOutputStream(objSocket.getOutputStream());
objOutputStream.write(("========================\nPROVA STAMPA\nHELLO SLOW SUD\n========================\n").getBytes());
objOutputStream.close();
objSocket.close();
I have created my first test application implementing a socket server. I am having some issues getting the client to receive data, but the server gets data just fine. Here is the server:
ServerSocket socket = new ServerSocket(11111);
System.out.println("CREATING SERVER...");
while (true) {
Socket SERVER_WORK = socket.accept();
BufferedReader clientIN = new BufferedReader(new InputStreamReader(SERVER_WORK.getInputStream()));
PrintWriter outSend = new PrintWriter(SERVER_WORK.getOutputStream());
String ClientSTR = clientIN.readLine();
System.out.println("Client 1: " + ClientSTR);
String toClient = "Hello";
outSend.write(toClient + '\n');
}
And here is the client:
System.out.println("CONNECTING TO SERVER...");
while (true) {
Socket clientSocket = new Socket(server, 11111);
BufferedReader fromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream toServere = new DataOutputStream(clientSocket.getOutputStream());
Scanner in = new Scanner(System.in);
toServere.write(in.nextLine().getBytes());
if (fromServer.ready())
System.out.println(fromServer.readLine());
clientSocket.close();
}
Everything works properly except for the client receiving data.
I found the solution: I needed a '\n' at the end of the line for the DataOutputStream/PrintWriter for the BufferedReader to work properly.
Wanted to know with java sockets whats the difference between the two. Trying to understand how to make a java server socket code using lessons from oracle and this is what it shows at the momment on their tutorials
int portNumber = Integer.parseInt(args[0]);
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
-Reader and -Writers are character-based and streams are byte-based.
In your example. You use writer when you want to write something to the socket and reader when you read something from the socket.
Hank