I have seen a lot of tutorials on client/server chat rooms using sockets, I am trying to create a instant messenger which will allow users (stored in a sql db) to chat with there contacts and groups(also stored on sql db). now I am really puzzled where to start. how would i go about creating a server which can handle peer to peer chat and group chat. I am using a mysql database which will store the user data and contacts list.
To get you started on ServerSocket and ClientSockets for multiple clients you can refer to the below post.
Two Socket sharing a port
Ideally every client would have only 1 socket connection to the server. To differentiate between your chats you can very simply use a unique identifier which will help differentiate between the different chat types.
You will need to create a multi-threaded socket server, this will accept incoming connections on a loop, and then pass off all operation between that instance of the socket and the client into a separate thread. This will allow you to run multiple client connections at once. This Page goes into great detail about creating both single and multi-threaded chat servers.
Related
While creating a Secure Instant messenger in java , if there are many clients
for example
3 clients
client A,B,C
A wants to connect to B and C both. So do i need to create different socket connection for both of them separately?
If so than is not it is a restriction like if there are 10,000 clients and each wants to connect to rest of them so i will need million ports?
Yes, your solution would work. But when you add many more users, the instant messager will lag and be hard to maintain.
The better solution is to have one connection for each client, a connection to a server. clients will connect to this server (which will also handle authentication), and the server will sort out the messages and deliver them to another client(the messages destination).
I'm developing a chat application in Java that enables client to connect to a predefined port and when two clients are connected the server should connect those two sockets and data should be exchanged between them.
I know to create a ServerSocket and Socket that will connect and establish a communication between them through a separate port and a server socket. But, how can I connect those two connected clients?
If you are creating a chat application then Chat server will not connect client socket in order to communicate.
You can try following approach:
On server side Use a HashTable to store connected clients.
When you receive a message from client, include a an id of CLient to whom message is to be sent and simply pass on the message to other client.
There will be separate threads running for these clients, so you need to have a policy to control the load on server.
This questions sounds like a homework assignment.
If you would like to see a great contemporary demo of a chat system you may learn from Play's WebSocket chat sample application.
First of all sorry for the long winded title but i was unable to think of a suitable title, considering my question.
Now to the problem. I am creating a peer to peer chat application, which has the ability to send and receive files while also being able to chat to individual contacts.
I understand i can capture the ip of the client connecting to the server and store this, then when that user starts a chat session to another person. I can use that stored ip to create a connection between the two clients using the ServerSocket.
BUT i do not wish to pass one users ip to another users computer for security reasons so, what i am asking basically is there a way to connect two clients together without passing each client each others ip.
so for e.g
(all sockets have read / write buffers )
Client 1-- logs in --> Server ( a session ID 1 is created between the client 1 and server )
Client 2-- logs in --> Server ( a session ID 2 is created between the client 2 and server )
Client 1 --- Starts chat with client 2 ---> Server ( server connects session 1 and session 2)
Client 1 can then chat and send files to client two without passing it the ip.
I am sorry if this is a unclear question or even stupid but i could not think of a way to even Google this question.
You would need to run the connection through a server. You could use the server to buffer the connection between the two clients and connect their communications in a way that would seem nearly seamless.
For example you could create a thread that, as long as both clients are connected, waits for input from client 1 and immediately pipes it to client 2. And the same would be necessary vice-versa. Is this the kind of solution you were looking for?
I have a spreadsheet application in Java, and one of the features it provides (which I developed) is sheet sharing. Basically, anyone can be a client or a server because the app has both server and client code. The user who is the server creates the share, specifies the IP, and then the share is created and active (best case scenario) with the server listening for clients on its IP and selected port.
At the moment, the client needs to enter the IP and port of the server that's listening in order to connect. The server then creates a new socket for that client and communicates with in on a separate thread, while the server continues listening on another (traditional TCP behavior). This is all working fine.
What I need to develop is auto-discovery, e.g. a client does not need to type in an IP or port, they simply select 'Join a share...' from the menu and then it starts looking for servers. When one is found, it should send its list of active shares on that IP. The user then selects which share to join from the list, and is connected.
However, I have doubts on how to tackle this issue. Should I use broadcast to poll servers, like DHCP does? Or is there an easier way?
What I'd like to implement is:
Client -> polls local network -> finds a server -> server sends active share list to client -> client selects share to join -> connected!
Technically, what you're looking for is active servers that are running your spreadsheet application.
One possibility would be for your server code to send out an "alive" message to the network every so often (say every 15 seconds). Your client code would listen for these "alive" messages, and produce a meaningful list of spreadsheet servers.
Another possibility would to to use the same database engine that you're using to store the spreadsheets to store the IP and port of the connected server code. The client code would just read the database table to get the connections.
I'm making a java program & I want this to be both as server and a client (using sockets). How is this best achieved?
If you mean that you want to both send and receive data, a single regular socket (on each computer) will do just fine. See Socket.getInputStream and Socket.getOutputStream.
The usual "server" / "client" distinction just boils down which host is listening for incoming connections, and which hosts connect to those hosts. Once the connection is setup, you can both send and receive from both ends.
If you want both hosts to listen for incoming connections, then just set up a ServerSocket and call accept on both hosts.
Related links:
Official trail: The Java™ Tutorials, Lesson: All About Sockets
If you want each station to function as a server and a client, like a p2p chat,
you should implement a thread with a ServerSocket, listening for incoming connections, and once it got a connection, open a new thread to handle it so the current one will keep on listening for new connections.
For it to be able to connect to others, simple use SocketAddress and Socket, in a different thread to try to connect to a specified server address (e.g. by a list of the user's friends)
you can find plenty of chat examples by googling.
cheers.
If you want the program to perform the same operations regardless of whether it is a server or a client for a certain connection, I could imagine handing off both the client Socket and the ServerSocket.accept()-produced socket to the same method for processing.
Have a look at jgroups it's a library that allows the creation of groups of processes whose members can send messages to each other. Another option would be to use hazelcast...
You may also look at this question.
The best way to do this is to run the server on a thread:
You run server.accept() therefore while your program is listening for a connection on that thread you can do whatever you want on the main thread, even connect to another server therefore making the program both a server & a client.