Plz help me with Java networking | Sololearn: Learn to code for FREE!

+4

Plz help me with Java networking

I made two programs and run both of them simultaneously on two command prompts. The command prompt which runs Server code is intended to print "Yo hello" after the Client (code given) is connected to it. But I'm getting NoSuchElenentException on nextLine() method(11th line of Server code) and I don't know what's wrong with it. Any help with explanation is greatly appreciated :) https://code.sololearn.com/ckwN4XJBvWex/?ref=app https://code.sololearn.com/cG0nLvf9OON2/?ref=app

11/30/2021 2:58:43 AM

Rishi

16 Answers

New Answer

+2

This might help you https://www.javatpoint.com/socket-programming

+3

🌀 Shail Murtaza شعیل مرتضیٰ thank you so much I found the error in my code. My code worked fine when I closed the output stream in the client code (https://www.sololearn.com/post/1403415/?ref=app pw.close() after writing message). But I don't know why I get error when I don't close it. Do you know the reason?

+3

zemiak Yes I'm running server first. And I've found a lot of stuff after many trails. The message only is delivered when the output buffer from client is flushed. wow

+2

You tried changing the localhost to 127.0.0.1 or InetAddress addr = InetAddress.getByName(null/“localhost”) then pass this to client Socket with port https://code.sololearn.com/cqpqGo8Wc2nR/?ref=app *change server port to 8080 **good example of sockets with explanations https://www.nakov.com/inetjava/lectures/part-1-sockets/InetJava-1.4-TCP-Sockets.html

+2

Kamen Studentov I'm not understanding what you're saying. What changes do I make?

+2

I try Rishi codes on SoloLearn by thread and it works main() is renamed to mainServer() class Test { public static void main(String[] args) throws Exception { new Thread( ()-> { try { Server.mainServer(args); } catch( Exception ex) { System.err.println( ex); } }).start(); Client.mainClient(args); } }

+1

Using a while loop try to make the server wait for a client to connect and if it does print the client message if it was sent.

+1

zemiak You used some lambda function and I don't know it😅 So can you give me that code without using lambda expressions plz :)

+1

ok, without lambda new Thread( new Runnable() { public void run() { ... } }).start();

+1

Rishi I don't know about sockets in java. but in C But in I have to bind the port and IP, which you have done. Then start listening. Then accept a connection from client. Edit: And in client side I have to bind port and IP and then connect. you have just bind it. this might help you. and this is the same procedure of sockets in PYTHON and C++ too. I think you have to do same thing in java but not sure.

+1

Rishi I'm saying that because socket implementation of any language depends upon the operating systems. This is not language dependent. That is why I think you are missing some steps in server and client files. These are the same procedure for both Windows and Linux. Not sure about Mac. Never used it

+1

🌀 Shail Murtaza شعیل مرتضیٰ zemiak Kamen Studentov Can you find any errors in these? https://www.sololearn.com/post/1403417/?ref=app https://www.sololearn.com/post/1403415/?ref=app https://www.sololearn.com/post/1403414/?ref=app

+1

run server first, how you do it ?

+1

Since you are recieving data from your client the issue may be with the Scanner. Have you tried using BufferedReader instead?

+1

Kamen Studentov no, I'll try that right away

0

Kamen Studentov In the server code, the accept() method at line 9 waits until the client is connected and only then resumes the command to next line. So that's not the problem in my code