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.