+1

I need some help in java related to networking!

I am comfortable with java, but a total beginner with networking. I have successfully learned to establish a server and a client, but they seem to work only when they are started on the same PC. I want a server app which would work on my PC and a client which would work on a device connected to the same network.

11/22/2019 3:56:40 PM

Anirudh Sharma

24 Answers

New Answer

+4

and to specify it its normally just a string. Replace "localhost" to "[your ip]"

+5

Ah I see Well networking is a pretty big subject. If you know nothing about networking but still write code for it, you'll have a bad time with bugs and errors. I highly suggest you learn about networking before implementing anything. Type of protocol, network speed, packet size, database (if used) and other stuff really matter

+4

yes, every device that connects to your router has a registered IP that looks like: 192.168.1.xyz On Windows cmd use ipconfig, linux use ifconfig, or log in to your router and find out your device's ip the code doesnt look wrong, but, if you didnt know, the server should run before any client lol its fine

+4

Youre welcome :) Pausing threads at a constant time can be risky because threads work on their own and get their own work down at their own time. They could lock up, and if one locks up, it could close off access to resources another new thread is waiting to use, thus causing an infinite lock. However, it is good to pause/sleep some threads to give the cpu some power to work on something else (if youre using an infinite loop). Threads are another big subject on their own lol but not too big, probably 1 to 2 chapters of most average sized books. What some people do is they retrieve data from the client every X seconds instead of using threads. Honestly IDK if this is a better method, but just an idea.

+4

I remember those days haha And thanks, im not awesome but glad to meet you too c:

+3

This is router dependant but you will have to forward a port on your local device where your server is hosted e.g. 192.168.1.xyz Then give, or hard code, your ISP given IP address + port to the client (or client's source code if hard coded). Dynamic IPs change, both device and ISP given, so hard coding may not be the best idea.

+3

Oh im sorry I misread the last part. If its on the same network, you would use the devices IP and port, and in 90% of cases, no you would not have to port forward And port forwarding is router related, but some ISPs do block ports, especially common ones (for safety reasons i guess)

+3

I took higher education for this stuff lol If you use threads yes that would work Hm didnt expect a single loop to affect it that way, thats interesting Ah sweet thats good ;) I learned networking as a whole seperate class lol Yay! Youre welcome :)

+2

I would only say one thing. As a student whose test paper contains questions worth 6 marks of writing such programs where you just have to take input 3 numbers and out their sum, all in terminal(no GUI), i am glad to meet such an awesome person...😁

+1

Thanks a lot, but this noob is literally a "noob" in networking. This is totally a new topic for me. I know much about IPs, how to get'em, but how to specify it to the program in code? Any how I have uploaded the project on github (as Minecraftian14). Its a simple, small project. If you wish, you may have a look. But logically, its almost the same thing...

+1

Thanks, will report when i get my lappy back!

+1

As per your prev post, i know networking is a big topic, but not for me. Actually, if one can solve my doubts or there exists good tutorials, i can directly start on a small project. Three days ago, all i knew about networking was internet, router, www.... But now i play with ports and IPs... Thanks a lot, am really happy you spent so much time to answer it.

+1

As per your prev post, i know networking is a big topic, but not for me. Actually, if one can solve my doubts or there exists good tutorials, i can directly start on a small project. Three days ago, all i knew about networking was internet, router, www.... But now i play with ports and IPs... Thanks a lot, am really happy you spent so much time to answer it.

+1

Further, one last thing. If i introduce a 0.1s pause to every loop, such as the two threads used for input and output ( in the project i uploaded), will it help? Till now it seems to be saving processing time in situations when something goes wrong.

+1

You seem to know a lot!! But how do you make a call to retrieve data "every X seconds"?; won't that require Thread.sleep(X*1000);?

+1

I wanted to use threads because they act as separate process(that's their use in most cases): one for input and the other for output. However if i set input and output in one single loop (whether in thread or not; will have less effect) there comes a moment when the server is sort of deadlocked, while the client is waiting for server's response, we see that's what server was doing- waiting for client's response....

+1

I have done threads as a part of three chapter in my java course. Only networking was not done.

+1

OMG! It worked, thanks a lot @「HAPPY TO HELP」. :)

0

Sorry if i am wrong, but do we have to port forward, even for private local servers which expects clients connected to the same network? I am pretty sure we have no such port forwarding done, but we still can play games like mini militia, minecraft, dual... Is port forwarding related to my router and ISP or my application-only?

0

Hmm, "to use the devices IP and port"?. Can you please help this noobie along this code: 1): Does it require some editing regarding adding the IPs and ports. Or 2): The code is wrong and i should use some other way. https://code.sololearn.com/c0tq7SSXAL4J/?ref=app https://code.sololearn.com/cZXwGBRTmhWV/?ref=app