#Client server creating multiple sockets

1 messages · Page 1 of 1 (latest)

little quail
#

Hi I am trying to make a client server but I need to make sure that clients can only chat to each other only if they know their IP addresses therefore I think I need to create multiple sockets for each user but I dont know how to can someone help me

night novaBOT
#

<@&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>.

#
TJ-Bot
Hi I am trying to make a client server but I need to make sure that clients can only chat to each other only if they know their IP addresses therefore I think I need to create multiple sockets for each user but I dont know how to can someone help me

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:

  1. Create a ServerSocket object to listen for incoming client connections.
  2. Accept incoming client connections using the accept() method of the ServerSocket class.
  3. For each accepted connection, create a new Socket object to handle communication with that specific client.
  4. Assign a separate thread for each client connection to handle communication asynchronously.
  5. 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!

hard vector
#
  • 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?
little quail
#

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

hard vector
#

Ok. Your TCP will create a separate socket for each client connection for you. You don't need to do that your self

little quail
#

Do you have an example code of a TCP?

hard vector
#
  • 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?
hard vector
little quail
#

Well from my lectures I was taught to use a central client server

hard vector
#
  • 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
little quail
#

I am unfamiliar with peer to peer

little quail
#

But I would like to know how to take that approach

hard vector
#

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

olive harness
#

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.

lone raft
#

so here's a basic pattern

hard vector
#

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

lone raft
#

i know from a bunch of questions like this that its best to just give an example

olive harness
#

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).

lone raft
#

give me a moment to make one

#

I assume your course is using ServerSocket and Socket

#

right?

hard vector
little quail
#

Sorry for making it so hard

#

Just finding it difficult to know how to send messages to just certain Ip address

hard vector
hard vector
little quail
#

Do u know how I can check if there is multiple sockets

hard vector
#

Use ss command to check TCP connections

#

Actually use lsof command, on your server process to see all the sockets

little quail
#

Could u actually show me where I am new to java

lone raft
#

in what your teacher gave you

#

did they use Serializable and ObjectInputStream

#

or did they use DataInputStream

hard vector
little quail
#

They did say to turn the sockets to input and output

lone raft
#

hmm

#

can you just search for those words?

hard vector
little quail
lone raft
#

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

little quail
#

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

lone raft
#

ah so he left what bytes you send as an "exercise for the reader"?

little quail
#

Yh

lone raft
#

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

little quail
#

Okay

lone raft
#

cool. okay give me some more time. it does take a bit to type this all out, I should save it for next time

little quail
#

Its fine

lone raft
#

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

little quail
#

Thanks bro thanks for the understanding I think I know what to do