0

How can I create my custom socket in java, to send information directly from the socket, for example, the socket I can see the client IP, but what if, instead of the IP I wanted to send the user name that is connected, or X other information, how about it?

Server:

public class Server {

public static void main(String[] args) throws IOException {

    ServerSocket ss = new ServerSocket(55555);
    Socket client = ss.accept();

    System.out.println(client.getInetAddress().getHostAddress());

    ss.close();
    client.close();
    }
}

Client:

public class Client {

public static void main(String[] args) throws UnknownHostException, IOException {

    Socket s = new Socket("195.168.50.7", 55555);

}

}

Result:

195.168.50.155

My question:

MyServer:

public class MyServer {

public static void main(String[] args) throws IOException {

    MyServerSocket ss = new MyServerSocket(55555);
    MySocket client = ss.accept();

    System.out.println(client.getMyInformationX());

    ss.close();
    client.close();
    }
}

MyClient:

public class MyClient {

public static void main(String[] args) throws UnknownHostException, IOException {

    MySocket s = new MySocket("192.168.50.7", 55555);
    }
}

My result:

myInformationX

EDIT:

More details: I wonder how I can send directly from the socket instantiates the user who is logged on the machine, without using outputstream or inputstream in the same way that I can get the IP without using outputstream or inputstream.

The user opens the application, the application creates the mysocket, the mysocket communicates with myServidor the diremante myServer the mysocket return the user who is logged in.

Something like:

mySocket.getUserLogged ()

1
  • 1
    You can read/write bytes from/to socket. If you want higher abstraction you can wrap it into some Reader/Writer, but for now it isn't clear what you exactly want. Commented Oct 28, 2016 at 17:33

2 Answers 2

1

What you want to do must not be as simple as it seems. So you might better take a different approach.

As your illustration shows, you noticed that you will have to customize not only the Socket class but the ServerSocket class as well... and most likely SocketImpl class as well

And the SocketImpl class declaration is like: public abstract class SocketImpl implements SocketOptions

So it's an abstract class and therefore you will have to provide the redefinition for many methods. This is very prone to error and will probably causes you some headaches. Your MyServerSocket will have to override the accept method to something like

public Socket accept() throws IOException {
         if (isClosed())
              throw new SocketException("Socket is closed");
           if (!isBound())
                throw new SocketException("Socket is not bound yet");
            Socket s = new MySocket((MySocketImpl) null);
            implAccept(s);
            return s;
     }

and as you can notice we return a MySocket instance instead of a Socket instance and you can see a MySocket constructor that receives a MySocketImpl parameter

But you will still have to adapt much more code.

So instead why don't you handle the interaction between the server and the socket using the sockets InputStream (to read) and OutputStream to write. Like in this example:

  while (true) {
     try {
     // Wait here and listen for a connection
     Socket s1 = s.accept();

     // Get output stream associated with the socket
     OutputStream s1out = s1.getOutputStream();
     BufferedWriter bw = new BufferedWriter(
     new OutputStreamWriter(s1out));

     // Send your string!
     bw.write(“Hello Net World!\n”)

     // ...
Sign up to request clarification or add additional context in comments.

Comments

0

TL;DR; Java's socket doesn't have a concept of "username". You should design your own protocol based on socket communication, and collection the username from client side system(application?) and send it over to server as socket's payload.

Longer version

Java's Socket connection handles transport layer and it doesn't have concept of username or password...means it doesn't care about its payload. It only care about "connecting" and "sending/receiving" between server and client. Once it's connected, it's up to you what information(payload) you'd like to send and receive.

You need to design your own protocol on this socket communication. You can just simply send fixed length of string byte stream that includes username or other information you want to send/receive. Or you can define a class that includes all the information you want to send, instantiate it, and send it over socket. This case you need to serialized and deserialize the object. There are a few alternatives instead of using Java's serialization/deserialization, e.g, Google' protocol buffer.

If you don't want to deal with socket's stream, reader, or anything, then you might want to take a look Google's gRPC, AMQP, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.