In: Computer Science
I have a problem with the code for my project. I need the two clients to be able to receive the messages but the code I have done only the server receives the messages. I need the clients to be able to communicate with each other directly not throught the server.
The requirements of this "local chat" program must meet are:
1. The chat is performed between 2 clients and a server.
2. The server will first start up and choose a port number. Then the server prints out its IP address and port number.
3. The client then will start up and create a socket based on the information provided by the server.
4. Now the client and the server can chat with each other by sending messages over the network.
5. The client/server MUST be able to communicate with each other in a continuous manner. (e.g. One side can send multiple messages without any replies from the other side)
6. There is no requirement on what language you use to implement this project, but your program should call upon the socket API for UDP to realize the functionality.
7. You should demonstrate your project using two machines (one for the client and one for the server. Pick your virtual machine as the client to communicate with your actual machine, which is server in our project).
8. You need to open at least 2 cmd window in your virtual machine to demonstrate multiple machine communicate with your server machine (actual machine)
My Code
UDPServer.java
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPServer {
public static void main(String args[]) throws
Exception{
int serverport=6789;
int clientport=1234;
byte[] receiveData = new
byte[1024];
byte[] sendData = new
byte[1024];
DatagramSocket serverSocket = new
DatagramSocket(serverport);
System.out.println("SERVER
READY");
while(true){
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String msg = new
String(receivePacket.getData()).trim();
System.out.println("Message from
CLIENT with ID:"+msg);
InetAddress IPAddress =
receivePacket.getAddress();
int port =
receivePacket.getPort();
String returnMsg = "Thanks.
Received your message!";
sendData =
returnMsg.getBytes();
DatagramPacket sendPacket = new
DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}
UDPClient.java
package socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient {
public static void main(String args[]) throws
Exception{
int serverport=6789;
int clientport=1234;
byte[] sendData = new
byte[1024];
byte[] receiveData = new
byte[1024];
BufferedReader inFromUser = new
BufferedReader( new
InputStreamReader(System.in));
String clientId;
System.out.println("Enter your
ID:");
clientId =
inFromUser.readLine();
while(true) {
System.out.println("Enter your message:");
String sentence
= inFromUser.readLine();
DatagramSocket
clientSocket = new DatagramSocket();
InetAddress
IPAddress = InetAddress.getByName("localhost");
sendData =
(clientId+":"+sentence).getBytes();
DatagramPacket
sendPacket =
new DatagramPacket(sendData,
sendData.length, IPAddress, serverport);
clientSocket.send(sendPacket);
DatagramPacket
receivePacket =
new
DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String
receivedMsg = new String(receivePacket.getData()).trim();
System.out.println("FROM SERVER:" + receivedMsg);
clientSocket.close();
}
}
}
UDPClient.java
import java.io.*;
import java.net.Socket;
public class ChatClient {
static Socket s;
static int port;
static String name;
public static void main(String[] args) {
Receivemsg1 receive = new Receivemsg1();
Sendmsg1 send = new Sendmsg1();
Thread receiver = new Thread(receive);
Thread sender = new Thread(send);
try{
port = Integer.parseInt(args[2]);
s = new Socket(args[1], port);
name = args[0];
System.out.println("Connected to: " +
s.getRemoteSocketAddress());
System.out.println("Welcome " + name + "!");
System.out.println("** Note: Type 'bye' and press Enter to
disconnect **");
System.out.println(">You are ready to start your
chat!\n");
receiver.start();
sender.start();
}catch(IOException e){
e.printStackTrace();
}
}
}
class Sendmsg1 implements Runnable {
@Override
public void run() {
String input;
PrintWriter out = null;
try {
out = new PrintWriter(ChatClient.s.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try {
while (!(input = br.readLine()).equals("bye")){
out.println(input);
}
out.println("Client disconnected");
ChatClient.s.close();
} catch (IOException e) {
System.out.println("Disconnected");
}
}
}
class Receivemsg1 implements Runnable {
@Override
public void run() {
String line;
BufferedReader in = null;
try {
in = new BufferedReader(new
InputStreamReader(ChatClient.s.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
while((line = in.readLine()) != null) {
if(line.equals("Server disconnected")){
System.out.println("> Server: bye");
System.out.println(line);
break;
}
}
} catch (IOException e) {
System.out.println("Disconnected");
}
}
}
UDPServer.java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer
{
static int port;
static ServerSocket ss;
static Socket s;
public static void main(String[] args)
{
Receivemsg receive = new Receivemsg();
Sendmsg send = new Sendmsg();
Thread receiver = new Thread(receive); //Creating thread
variables
Thread sender = new Thread(send);
try{
port = Integer.parseInt(args[0]);
ss = new ServerSocket(port); //connected to given port number
System.out.println("Server Started with port: " +
ss.getLocalPort());
s = ss.accept();
System.out.println("Connection Established with client: " +
s.getRemoteSocketAddress());
System.out.println("** Note: Type 'bye' and press Enter to
disconnect **");
System.out.println(">You are ready to start your
chat!\n");
receiver.start(); //receiver thread invoked
sender.start(); //sender thread invoked
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
class Sendmsg implements Runnable {
@Override
public void run() {
String input;
PrintWriter out = null;
try {
out = new PrintWriter(ChatServer.s.getOutputStream(), true);
//store what is to be sent as output through port
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try {
while (!(input = br.readLine()).equals("bye")){
out.println(input);
}
out.println("Server disconnected");
ChatServer.s.close();
} catch (IOException e) {
System.out.println("Disconnected");
}
}
}
class Receivemsg implements Runnable {
@Override
public void run() {
String line;
BufferedReader in = null;
try {
in = new BufferedReader(new
InputStreamReader(ChatServer.s.getInputStream())); //read the
message from the socket
} catch (IOException e) {
e.printStackTrace();
}
try {
while((line = in.readLine()) != null) {
if(line.equals("Client disconnected")){
System.out.println("> Client: bye");
System.out.println(line);
break;
}
}
} catch (IOException e) {
System.out.println("Disconnected");
}
}
}
Check this! This needed lot of work and finally here!
:-)