简体   繁体   中英

Creating sessions in java socket programming with one server and multiple clients

I am a novice programmer in java. I have created a program which is similar to a chat application using socket programming. I haven't used threads.

My question is:

Whenever a client1 wants to communicate to another client2 via the sever how can i know which client is communicating.

I thought if i could differentiate all clients from server side by using sessions it would be easier. Just a random thought.

why this question??

I want to know this because i have stored the adressess of clients in a file along with a sequence number to make the messages visible to the pair alone globally. Whenever a client sends a message it prefixes it with a sequence number which is got from the client1 client2 pair stored in that file.

Any help would be greatly appreciated. Im just a learner in java. I apologize for any obscurity in my question.

First of all, try to make a thread to open a socket because it helps in your GUI processing. And the point that you want to see client names can be done by this steps:-

Add an editbox and enter your name (client name) and send this name along with the message and at server-side, split message and name so that you can determine which message is sent by whom.

Ex:- Client name is Alex and the message is "hi" then the data which will be sent is something like "Alex+hi". Now split this message using split() function in java at the server-side. Hope this encoding and decoding will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM