+ 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

30th Nov 2021, 2:58 AM
Rishi
Rishi - avatar
16 ответов
1st Dec 2021, 5:16 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 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?
1st Dec 2021, 6:19 AM
Rishi
Rishi - avatar
+ 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
1st Dec 2021, 7:06 AM
Rishi
Rishi - avatar
+ 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
30th Nov 2021, 6:44 AM
Kamen Studentov
Kamen Studentov - avatar
+ 2
Kamen Studentov I'm not understanding what you're saying. What changes do I make?
30th Nov 2021, 7:09 AM
Rishi
Rishi - avatar
+ 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); } }
30th Nov 2021, 12:09 PM
zemiak
+ 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.
30th Nov 2021, 5:37 AM
Kamen Studentov
Kamen Studentov - avatar
+ 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 :)
30th Nov 2021, 3:46 PM
Rishi
Rishi - avatar
+ 1
ok, without lambda new Thread( new Runnable() { public void run() { ... } }).start();
30th Nov 2021, 9:19 PM
zemiak
+ 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.
1st Dec 2021, 4:23 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 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
1st Dec 2021, 4:29 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
run server first, how you do it ?
1st Dec 2021, 6:20 AM
zemiak
+ 1
Since you are recieving data from your client the issue may be with the Scanner. Have you tried using BufferedReader instead?
1st Dec 2021, 7:41 AM
Kamen Studentov
Kamen Studentov - avatar
+ 1
Kamen Studentov no, I'll try that right away
1st Dec 2021, 10:36 AM
Rishi
Rishi - avatar
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
30th Nov 2021, 5:41 AM
Rishi
Rishi - avatar