Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SocketPro/src at main · Udesh-Ranjan/SocketPro · GitHub[^]

Its a link to the source . There are two class files one is SockServer and the other one is SockClient. It runs ok in localhost I would like to run the server on my pc and the client in my girlfriend's pc .

I know the code looks dumb in my repo I will scale it once I know the way to connect two computers through socket in java.

What changes do I have to do and what is the correct way and I would also like to know is there other better alternatives to socket in java?

English is not my native language excuse me please :)

What I have tried:

Please visit the link above.
Posted
Updated 2-Jan-21 13:41pm

1 solution

Quote:
It runs ok in localhost I would like to run the server on my pc and the client in my girlfriend's pc .
If the code works fine, then the next step is to get a static IP for your PC and then connect to the SockServer instance using that IP address and port.
Quote:
is there other better alternatives to socket in java?
I would recommend using anything other than native-JDK socket programming. You can easily find a high-level networking SDK in Java and utilize it. The benefit that you get is that the framework will not break and throw exceptions too much; they will be handled by the author of the SDK/library for you. Sometimes you get a lot of features (broadcast, connection handling, graceful connection closing, etc.) that you will need to write yourself, thus reinventing the wheel.

One of such frameworks is Spring that allows you to ignore the underlying complexities of Java, and work on the business logic: Getting Started | Using WebSocket to build an interactive web application[^].
GitHub - DrRoach/NetworkAPI: Simple Java network API[^] (although this library has the same methods that you have already written, maybe some exception handling is done)
Simple Network Framework for Java[^]
GitHub - EsotericSoftware/kryonet: TCP/UDP client/server library for Java, based on Kryo[^]
A Guide to Java Sockets | Baeldung[^] (if you want to continue using native-Java Socket)

The goal is, as long as the client's PC is able to reach out to the server (using IP address and port), your app is going to work just fine—and the string should be delivered easily.
 
Share this answer
 
Comments
Udesh-Ranjan 2-Jan-21 21:16pm    
@Afzaal Ahmad Zeeshan thanks for the such a detailed and helpful
explanation.
Afzaal Ahmad Zeeshan 2-Jan-21 23:33pm    
Welcome!

If the answer helped, please mark it as a solution as well. :)
Udesh-Ranjan 3-Jan-21 2:04am    
@Afzaal Ahmad Zeeshan if we use ip address which will change It will cause problem and making it static is as well can we use something else through which we can send string from one system to another.
Afzaal Ahmad Zeeshan 3-Jan-21 10:45am    
Without the proper (static) IP address knowledge, it depends on where the PCs are. If on the same network, then you might be able to connect using the hostnames, otherwise, you cannot connect at all.
Udesh-Ranjan 3-Jan-21 21:11pm    
thanks @Afzzal Ahmad Zeeshan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900