Activetoday
Hi there I'm trying to create a client/server chat program.i have three clients and one server, when a client sends some string to the server the server will send this string to the other clients.I have created the part where the client sends data to the server but I don't know how to send received data from a client to the other clients.
this is the code I created :
UDP (User Datagram Protocol). Java Sockets and Client/Server Programming. You can use Java to communicate with remote processes using a client/server model. A server listens for connection requests from clients across the network or even from the same machine. Clients know how to connect to the server via an IP address and port number.
For server:
Client Server Application Java
- Java socket client example. Let's work through a short example that executes an HTTP GET against an HTTP server. HTTP is more sophisticated than our example permits, but we can write client code to handle the simplest case: request a resource from the server and the server returns the response and closes the stream.
- This project consists of a simple UDP server and client programs. If you've never written a program that uses UDP, this is an ideal starting project. The server runs on a local computer, waiting for a datagram request from a remote computer asking for the server's current time. The server then.
for clients: (X is the number of client : client1/client2...)
Yukina YukinatteYukina Yukinatte
bumped to the homepage by Community♦4 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
2 Answers
It is a bit unclear how exactly your chat should work.But the simplest idea just to move forward is:
- Your server should keep the latest number (X) of messages received. It would be also good to store the time each message was received.
- Every time a client connects to the server, the server sends it the X latest messages.
2,01311 gold badge1313 silver badges1515 bronze badges
You could save the clients addresses in the server when they connect to it, then, every time a client sends a new message to the server, send that message to the other clients.
You could also use multicast. Make the clients join a multicast address, and send the new messages to that address from the server. That way you don't need to save addresses and every client can 'ear' the messages.
See http://docs.oracle.com/javase/1.4.2/docs/api/java/net/MulticastSocket.html and http://staff.www.ltu.se/~peppar/java/multicast_example/
miguel.gazelamiguel.gazela
Not the answer you're looking for? Browse other questions tagged javaclient-server or ask your own question.
Udp In Java
In this tutorial I have shared simple client server program example to explain java socket programming.
In this example I will use Socket and ServerSocket classes for connection oriented socket programming. Since data is shared between server and client over network in the form of streams so DataInputStream and DataOutputStream classes are used.
Java Socket Programming (Client Server Program)
Server
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 | importjava.net.ServerSocket; publicstaticvoidmain(Stringargs[]){ //create socket, 5000 is port number Socket socket=serverSocket.accept(); System.out.println('Client Connected...'); //fetch incoming message DataInputStream dis=newDataInputStream(socket.getInputStream()); serverSocket.close(); e.printStackTrace(); } |
Above code is used to create server which is running on localhost on port number 5000.
Client
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 | importjava.net.Socket; publicstaticvoidmain(Stringargs[]){ //localhost because server is running on local machine, otherwise use ip of server message=sc.nextLine(); //sending the message DataOutputStream dout=newDataOutputStream(socket.getOutputStream()); dout.flush(); socket.close(); e.printStackTrace(); } |
Since the server is running on local machine so I have used locahost while creating connection. Otherwise use the IP address of server.
How to Execute?
1. First open a command prompt and run server program. The server will wait for client to be connected.
2. Now open another command prompt and run client program. This will connect client with server. Enter a message at client side to send it to server.
See below screenshot as an example.
Comment below if you have any queries regarding above client server program in java.
![Udp client and server in java Udp client and server in java](/uploads/1/2/6/1/126156132/240818667.jpg)