I'm making a java program & I want this to be both as server and a client (using sockets). How is this best achieved?
-
You mean you are going to start the program twice and want the instances to communicate? or you plan to start more instances? or you program will be a server for some other program and a client for another one? – pgras Oct 25 '11 at 10:49
-
The program is going to run on two different machines and they will exchange data – Samantha Catania Oct 25 '11 at 15:18
5 Answers
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

- 413,195
- 112
- 811
- 826
-
I tried this code and it didn't work: import java.io.*; import java.net.*; public class Server { public static void main (String args[]) throws Exception{ ServerSocket Server = new ServerSocket(6000); Socket connected = Server.accept(); Socket clientSocket = new Socket("localhost", 6000); System.out.println(clientSocket.getInetAddress()); } } – Samantha Catania Nov 03 '11 at 21:07
-
1You need to split that program into two programs. Accept will block until a client connects! – aioobe Nov 04 '11 at 07:21
-
-
@SamanthaCatania then write your program to behave like two different programs (e.g. using commandline switches: `java -jar myapp.jar --server --port 60042` `java -jar myapp.jar --client --connect ipofserver:60042` – mbx Nov 17 '11 at 20:11
-
-
@SamanthaCatania the average user can't provide command line switches - come on. For the dumbest imaginable luser you can wrap that lines into two desktop links, or provide a dialig at start. – mbx Nov 17 '11 at 20:20
-
@mbx your average user doesn't know the difference between a server and a client – Samantha Catania Nov 17 '11 at 20:24
-
@mbx luck you; this is for a high school project, I was talking about my project with my class mates and were like "what's a socket?!" – Samantha Catania Nov 17 '11 at 20:33
-
So you want to start the same program on two different computers and magically make them start communicating with eachother? Will the user provide computer A with the ip-address for computer B? – aioobe Nov 17 '11 at 20:43
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.

- 18,195
- 4
- 41
- 71
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.

- 799
- 9
- 31
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.

- 156,901
- 35
- 231
- 235

- 5,116
- 5
- 39
- 69
-
OP said "The program is going to run on two different machines and they will exchange data" and this is the accepted answer?! – mbx Nov 17 '11 at 20:08
-
Maybe a closer look at your code would help to understand the (former) problem. Well, if it actually works and you're fine with the way it works you can spare to post the code. – mbx Nov 17 '11 at 20:15
-
@mbx it is actually quite simple; 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. – Samantha Catania Nov 17 '11 at 20:20
-
@SamanthaCantania that was the missing piece, server&client is a desing driven decision and not user driven – mbx Nov 17 '11 at 20:25