#Client server creating multiple sockets
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
need to create multiple sockets for each client connection. Can someone help me with how to implement this in Java?
To implement multiple sockets for client connections in Java, you will need to create a server that can handle incoming client connections and assign a separate socket for each client. Here is a basic outline of how you can achieve this:
- Create a ServerSocket object to listen for incoming client connections.
- Accept incoming client connections using the accept() method of the ServerSocket class.
- For each accepted connection, create a new Socket object to handle communication with that specific client.
- Assign a separate thread for each client connection to handle communication asynchronously.
- Implement logic within each thread to verify the IP address of the connecting client before allowing them to chat with other clients.
By following these steps, you can ensure that clients can only chat with each other if they know each other's IP addresses while using multiple sockets to manage simultaneous connections on the server side. If you need further assistance or code examples, feel free to ask for help in this discord server!
- Are you making a centralized chat application? Meaning it's client server not peer to peer.
- How does your client connect to server? Through UDP or TCP?
The client inputs their Ip address and port number to join the server and the clients must know each others Ip address and port number to text each other
There is also a coordanater that all clients have access to as the coordinator gives the clients the Ip addresses of other clients
TCp
Ok. Your TCP will create a separate socket for each client connection for you. You don't need to do that your self
Do you have an example code of a TCP?
- but Im still unclear about how your chatting service works. Is it peer to peer or client-server?
- When I send a msg to you, does this msg go directly to you or does it go to a centralized server first?
It's everywhere
Well from my lectures I was taught to use a central client server
- Like right now, I know my msg doesn't go directly to your device.
- My msg first go to Discord server then Discord server send the msg to you
I am unfamiliar with peer to peer
Ok now I see
I dont know how to do that I only know how to send messages to a group server
But I would like to know how to take that approach
Your program is not peer to peer now I know. So your original question is "invalid" because tcp connection already create a socket for talking to each client. You don't need to do that yourself
Peer to peer is really just client-server with only one client though. One side has the server-socket and one the client socket.
The difference is that with a central-server, only that server needs to have a known address and the various clients of the server don't see each other's addresses.
Thats fine
so here's a basic pattern
What your server needs to do is, upon receiving a msg from a client, forwarding it to the destination TCP socket, according to the destination IP address that comes along with the msg
i know from a bunch of questions like this that its best to just give an example
The challenge for peer-to-peer is that many people don't have an inbound network path open for the connection - they have to open a port and forward from their router to their machine (and remove any blocking firewall rules).
give me a moment to make one
I assume your course is using ServerSocket and Socket
right?
Yes you are right.✅
but it feels like cheating if you just directly give the solution😂
Sorry for making it so hard
Just finding it difficult to know how to send messages to just certain Ip address
Is there any real peer to peer msg apps still in use now? I don't know any. Yes peer to peer sounds hard to do
- Your server now has established bunch of TCP connections.
- Each connection has a socket that talks to a certain IP address
- When you want to forward a msg to a certain IP address, just scan all the sockets and find the right one to send msg to
Do u know how I can check if there is multiple sockets
Use ss command to check TCP connections
Actually use lsof command, on your server process to see all the sockets
Could u actually show me where I am new to java
so question:
in what your teacher gave you
did they use Serializable and ObjectInputStream
or did they use DataInputStream
They did say to turn the sockets to input and output
When the server is running, you can use lsof command like this to check which sockets the server is using
What do you mean
I mean
your teacher is asking you to use sockets to make a chat application
generally they also would tell you how to send data over those sockets
I am making an example using a way i've seen before
but there are a few different ways of sending data
so i want to use the one your teacher is using
He basically explained what sockets are server sockets the use of sockets like listening and accepting connections and talked about a byte strem for sending data
The rest was on threads and mulitithreads
ah so he left what bytes you send as an "exercise for the reader"?
Yh
okay so just to preface this
java comes with a builtin way of basically magically teleporting an object over the internet
it is horrific and something you shouldn't use in any serious code
but
its very easy to set up and you don't need extra dependencies
so the example i am making is using it
but I want you to know ahead of time that if you are making anything "real" you should come back and ask how to actually do this sort of stuff
Okay
cool. okay give me some more time. it does take a bit to type this all out, I should save it for next time
Its fine
okay here is a very basic client/server program
this doesn't include clients being able to talk to eachother yet, but its a starting place
theres a lot of moving pieces already though, so take some time to doodle with it and ask questions
Thanks bro thanks for the understanding I think I know what to do