Hey guys.

I've been programming in java for about a year now but I've just recently been learning socket programming before my last semester of my computer science class. I have to do a project and I was thinking about making a client/server program. I've landed on making an instant messenger for classroom use. I just have a few questions because right now I'm just learning socket programming from google, and I'm having some real trouble finding socket programming with multiclients.. I think I need to have the client input a string and pass it to the server, but how would I send that string to another specified client. I am using threads with my program. Any other info you need I can get you, thanks in advance.

lsquared

To make this easier, you might want to consider using a Server class that encapsulates a ServerSocket and has an array of Sockets.

The ServerSocket will be used to establish the Server, while the array of Sockets will be used for awaiting Clients (via ServerSocket.accept() ).

Attached is a poorly written Checkers project I made. The highlight of the project is how information is being sent and received between the CheckersDisplay classes and the ServerObject class.

To run the program, run the Server first then run the CheckersDisplay project twice (though, you may want to wait until one is showing first before loading the other).

Somewhere in the CheckersDisplay is a main with the CheckersDisplay constructor called. Change the String after you have one running so that the chat-box will have different users displayed instead of the same name between both clients.

Again it's poorly written but it should suffice for a good example of Client/Server programming in a nutshell.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.