Last active
May 6, 2020 10:24
-
-
Save code-n-roll/347b65f9d53fde9201590ed35cc0f2ab to your computer and use it in GitHub Desktop.
Chat implementation on UDP protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package network_tech; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.net.DatagramPacket; | |
import java.net.DatagramSocket; | |
import java.net.InetAddress; | |
public class ChatClient { | |
public static void main(String args[]) throws Exception{ | |
String host = null; | |
if (args.length < 1){ | |
System.out.println("Usage: java ChatClient <server_hostname>"); | |
System.exit(0); | |
} else { | |
host = args[0]; | |
} | |
DatagramSocket socket = new DatagramSocket(); | |
MessageReceiver receiver = new MessageReceiver(socket); | |
MessageSender sender = new MessageSender(socket, host); | |
Thread rt = new Thread(receiver); | |
Thread st = new Thread(sender); | |
rt.start(); | |
st.start(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package network_tech; | |
import java.io.IOException; | |
import java.net.DatagramPacket; | |
import java.net.DatagramSocket; | |
import java.net.InetAddress; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashSet; | |
public class ChatServer extends Thread{ | |
public final static int PORT = 7331; | |
private final static int BUFFER = 1024; | |
private DatagramSocket socket; | |
private ArrayList<InetAddress> clientAddresses; | |
private HashSet<String> existingClients; | |
private ArrayList<Integer> clientPorts; | |
public ChatServer() throws IOException { | |
socket = new DatagramSocket(PORT); | |
clientAddresses = new ArrayList<>(); | |
clientPorts = new ArrayList<>(); | |
existingClients = new HashSet<>(); | |
} | |
public void run(){ | |
byte[] buf = new byte[BUFFER]; | |
while(true){ | |
try{ | |
Arrays.fill(buf, (byte)0); | |
DatagramPacket packet = new DatagramPacket(buf, buf.length); | |
socket.receive(packet); | |
String content = new String(buf, 0, buf.length); | |
InetAddress clientAddress = packet.getAddress(); | |
int clientPort = packet.getPort(); | |
String id = clientAddress.toString() + "," + clientPort; | |
if (!existingClients.contains(id)){ | |
existingClients.add(id); | |
clientPorts.add(clientPort); | |
clientAddresses.add(clientAddress); | |
} | |
System.out.println(id + " : " + content); | |
byte[] data = (id + " : "+ content).getBytes(); | |
for (int i = 0; i < clientAddresses.size(); i++){ | |
InetAddress cl = clientAddresses.get(i); | |
int cp = clientPorts.get(i); | |
packet = new DatagramPacket(data, data.length, cl, cp); | |
socket.send(packet); | |
} | |
} catch (IOException e){ | |
System.err.println(e); | |
} | |
} | |
} | |
public static void main(String[] args){ | |
try { | |
ChatServer s = new ChatServer(); | |
s.start(); | |
} catch(IOException e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package network_tech; | |
import java.net.DatagramPacket; | |
import java.net.DatagramSocket; | |
class MessageReceiver implements Runnable{ | |
DatagramSocket socket; | |
byte buf[]; | |
MessageReceiver(DatagramSocket s){ | |
socket = s; | |
buf = new byte[1024]; | |
} | |
public void run(){ | |
while(true){ | |
try{ | |
DatagramPacket packet = new DatagramPacket(buf, buf.length); | |
socket.receive(packet); | |
String received = new String(packet.getData(), 0, packet.getLength()); | |
System.out.println(received); | |
} catch (Exception e){ | |
System.err.println(e); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package network_tech; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.net.DatagramPacket; | |
import java.net.DatagramSocket; | |
import java.net.InetAddress; | |
class MessageSender implements Runnable { | |
public final static int PORT = 7331; | |
private DatagramSocket socket; | |
private String hostname; | |
MessageSender(DatagramSocket s, String h){ | |
socket = s; | |
hostname = h; | |
} | |
private void sendMessage(String s) throws Exception{ | |
byte buf[] = s.getBytes(); | |
InetAddress address = InetAddress.getByName(hostname); | |
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, PORT); | |
socket.send(packet); | |
} | |
public void run(){ | |
boolean connected = false; | |
do { | |
try{ | |
sendMessage("GREETINGS"); | |
connected = true; | |
} catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} while (!connected); | |
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); | |
while(true){ | |
try{ | |
while (!in.ready()){ | |
Thread.sleep(100); | |
} | |
sendMessage(in.readLine()); | |
} catch(Exception e){ | |
System.err.println(e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment