├── .gitignore ├── Client.java ├── README.md ├── STUN.java ├── SendClient.java └── Server2.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | /ServerTCP.java 3 | -------------------------------------------------------------------------------- /Client.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Client side P2P chat 3 | * 4 | * Creates UDP connection to server 5 | * Gets IP and Port 6 | * Creates TCP connection with peer 7 | * listens on port 8 | * @author Nathan Kong, Ardeshir Bastani, Yangcha Ho 9 | * 10 | */ 11 | 12 | import java.io.*; 13 | import java.net.*; 14 | import java.util.*; 15 | import java.text.*; 16 | 17 | public class Client extends Thread{ 18 | 19 | String activity; 20 | static String myIp; 21 | static int myPort; 22 | static String peerIp; 23 | static int peerPort; 24 | 25 | public Client(String activity){ 26 | this.activity = activity; 27 | this.myIp = ""; 28 | this.peerIp = ""; 29 | } 30 | 31 | /************************************************** 32 | * runs the threads to listen to the port and talk to the peer 33 | */ 34 | public void run(){ 35 | try{ 36 | if(activity == "listen"){ 37 | peerListen(); 38 | }else{ 39 | peerSend(); 40 | } 41 | }catch(Exception e){ 42 | e.printStackTrace(); 43 | } 44 | 45 | } 46 | 47 | /************************************************** 48 | * Starts the program 49 | * @param args 50 | * @throws SocketException 51 | * @throws UnknownHostException 52 | */ 53 | public static void main(String[] args) throws Exception { 54 | //String serverName = "teamone.onthewifi.com"; 55 | String serverName = "10.0.0.232"; 56 | int port = 54545; 57 | 58 | // prepare Socket and data to send 59 | DatagramSocket clientSocket = new DatagramSocket(); 60 | //System.out.println(clientSocket.isBound()); 61 | clientSocket.setReuseAddress(true); 62 | 63 | 64 | byte[] sendData = "Hello".getBytes(); 65 | System.out.println("sending Hello to server"); 66 | 67 | // send Data to Server 68 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName(serverName), port); 69 | clientSocket.send(sendPacket); 70 | 71 | // receive Data 72 | DatagramPacket receivePacket = new DatagramPacket(new byte[1024], 1024); 73 | clientSocket.receive(receivePacket); 74 | System.out.println("received data from server"); 75 | clientSocket.close(); 76 | // Convert Response to IP and Port 77 | String response = new String(receivePacket.getData()); 78 | String[] splitResponse = response.split(":"); 79 | myIp = splitResponse[0]; 80 | myPort = Integer.parseInt(splitResponse[1]); 81 | peerIp = splitResponse[2]; 82 | peerPort = Integer.parseInt(splitResponse[3]); 83 | System.out.println("my Info: " + myIp + ":" + myPort ); 84 | System.out.println("peer Info: " + peerIp + ":" + peerPort ); 85 | System.out.println("\n\n"); 86 | 87 | //clientSocket.bind(myPort); 88 | //clientSocket.close(); 89 | 90 | //listen to port 91 | Thread listen = new Client("listen"); 92 | listen.start(); 93 | 94 | Thread.sleep(2000); 95 | 96 | //send datagram 97 | Thread send = new Client("send"); 98 | send.start(); 99 | 100 | //waits for thread to end 101 | listen.join(); 102 | send.join(); 103 | }//main 104 | 105 | /************************************************** 106 | * sends the p2p chat 107 | */ 108 | private static void peerSend() { 109 | 110 | try { 111 | //create socket 112 | //Socket mySoc = new Socket(peerIp, peerPort); 113 | Socket mySoc = new Socket(); 114 | mySoc.setReuseAddress(true); 115 | //mySoc.bind( new InetSocketAddress("myIp", myPort) ); 116 | mySoc.connect( new InetSocketAddress(peerIp, peerPort) ); 117 | //System.out.println(mySoc.isBound()); 118 | pause(); 119 | 120 | //create streams 121 | DataOutputStream out = new DataOutputStream( mySoc.getOutputStream() ); 122 | DataInputStream in = new DataInputStream( mySoc.getInputStream() ); 123 | 124 | //create and send message 125 | System.out.println("Sending to Socket: " + peerPort); 126 | String msg = getTime() + "\t" + myPort + ": Can you hear me?"; 127 | out.writeUTF(msg); 128 | System.out.println(msg); 129 | 130 | //get string from client B 131 | String newMsg = in.readUTF(); 132 | System.out.println(newMsg + "\n"); 133 | 134 | //close socket 135 | mySoc.close(); 136 | 137 | } catch (Exception e) { 138 | e.printStackTrace(); 139 | } 140 | } 141 | 142 | /************************************************** 143 | * The listens to the socket 144 | * @throws Exception 145 | */ 146 | private static void peerListen() throws Exception{ 147 | //create and listen to socket 148 | System.out.println("Listening on Socket: " + myPort); 149 | ServerSocket peerSocket = new ServerSocket(); 150 | peerSocket.setReuseAddress(true); 151 | peerSocket.bind( new InetSocketAddress(myIp, myPort) ); 152 | peerSocket.setReuseAddress(true); 153 | Socket peer = peerSocket.accept(); 154 | 155 | System.out.println("Just connected with peer"); 156 | 157 | //create a stream to talk to other peer 158 | DataInputStream in = new DataInputStream(peer.getInputStream()); 159 | DataOutputStream out = new DataOutputStream(peer.getOutputStream()); 160 | 161 | //get string from client A 162 | String msg = in.readUTF(); 163 | System.out.println(msg); 164 | 165 | //create a message and send it to Client A 166 | String newMsg = getTime() + "\t" +myPort+ ": Yes I can hear you!"; 167 | out.writeUTF(newMsg); 168 | System.out.println(newMsg + "\n"); 169 | 170 | //close socket 171 | peerSocket.close(); 172 | } 173 | 174 | /************************************************** 175 | * Creates a time stamp 176 | */ 177 | private static String getTime(){ 178 | DateFormat df = new SimpleDateFormat("hh:mm:ss"); 179 | Date dateobj = new Date(); 180 | return df.format(dateobj); 181 | } 182 | 183 | /************************************************** 184 | * Creates a random timer to wait 185 | */ 186 | private static void pause()throws Exception{ 187 | //create random number between 1 and 15 188 | Random rand = new Random(); 189 | int breath = rand.nextInt(5) + 1; 190 | Thread.sleep(breath*1000); 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # P2P-chat 2 | A peer to peer chat using java 3 | 4 | There are 2 parts to this project:
5 | 1.Client side
6 | 2.Server side
7 | 8 | The server gets turned on.
9 | Client 1 sends a datagram to the server through UDP.
10 | The server pulls the ip and port from the datagram.
11 | Client 2 sends a datagram to the server through UDP.
12 | The server pulls the ip and port from the datagram.
13 | The server sends a datagram to client 1 -> client1IP:client1PORT:client2IP:client2PORT:end
14 | The server sends a datagram to client 2 -> client2IP:client2PORT:client1IP:client1PORT:end
15 | 16 | Client 1 contacts Client 2 and sends a message "Can you hear me?"
17 | Client 2 recieves the message and sends a message "I can hear you."
18 | 19 | Client 2 contacts Client 1 and sends a message "Can you hear me?"
20 | Client 1 recieves the message and sends a message "I can hear you."
21 | -------------------------------------------------------------------------------- /STUN.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A UDP server 3 | * Listens to port 54545 and sends the ip addresses and ports to the clients 4 | * @author Nathan Kong, Ardeshir Bastani, Yangcha Ho 5 | * 6 | */ 7 | 8 | import java.net.*; 9 | import java.io.*; 10 | 11 | public class STUN{ 12 | 13 | public static void main(String[] args) throws IOException { 14 | 15 | //create a socket for udp on port 54545 16 | DatagramSocket serverSocket = new DatagramSocket(54545); 17 | 18 | while(true){ 19 | /** First client **/ 20 | //create packet to receive data 21 | System.out.println("Waiting for Clients on Port 54545..."); 22 | DatagramPacket receivePacket1 = new DatagramPacket(new byte[1024], 1024); 23 | serverSocket.receive(receivePacket1); 24 | 25 | //get address and port from datagram 26 | InetAddress p1IPAddress = receivePacket1.getAddress(); 27 | int p1Port = receivePacket1.getPort(); 28 | System.out.println("RECEIVED: " + p1IPAddress.toString() + ":" + p1Port); 29 | 30 | /** Second client **/ 31 | //DatagramSocket serverSocket2 = new DatagramSocket(54545); 32 | System.out.println("Waiting for Clients on Port 54545..."); 33 | //create packet to receive data 34 | DatagramPacket receivePacket2 = new DatagramPacket(new byte[1024], 1024); 35 | serverSocket.receive(receivePacket2); 36 | 37 | //get address and port from datagram 38 | InetAddress p2IPAddress = receivePacket2.getAddress(); 39 | int p2Port = receivePacket2.getPort(); 40 | String msgInfoOfClient2 = p2IPAddress.toString().substring(1) + ":" + p2Port + ":" + p1IPAddress.toString().substring(1) + ":"+ p1Port + ":end"; 41 | System.out.println("RECEIVED: " + p2IPAddress.toString() + ":" + p2Port); 42 | 43 | /** Send data to each other **/ 44 | //Structure of message -> myIp:myPort:peerIp:peerPort:end 45 | String msgInfoOfClient1 = p1IPAddress.toString().substring(1) + ":" + p1Port + ":" + p2IPAddress.toString().substring(1) + ":"+ p2Port + ":end"; 46 | 47 | // Send Information of Client2 to Client1 48 | serverSocket.send(new DatagramPacket(msgInfoOfClient1.getBytes(), msgInfoOfClient1.getBytes().length, p1IPAddress, p1Port)); 49 | 50 | // Send Information of Client1 to Client2 51 | serverSocket.send(new DatagramPacket(msgInfoOfClient2.getBytes(), msgInfoOfClient2.getBytes().length, p2IPAddress, p2Port)); 52 | } 53 | 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /SendClient.java: -------------------------------------------------------------------------------- 1 | import java.net.*; 2 | import java.util.*; 3 | 4 | public class SendClient { 5 | 6 | public static void main(String[] args) throws Exception { 7 | //String serverName = "teamone.onthewifi.com"; 8 | String serverName = "10.0.0.232"; 9 | int port = 54545; 10 | 11 | // prepare Socket and data to send 12 | DatagramSocket clientSocket = new DatagramSocket(); 13 | byte[] sendData = "Hello".getBytes(); 14 | System.out.println("sending Hello to server"); 15 | 16 | for (int i=0; i<30; i++){ 17 | // send Data to Server 18 | DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName(serverName), port); 19 | clientSocket.send(sendPacket); 20 | Thread.sleep(1000*i); 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Server2.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A UDP server 3 | * Listens to port 54545 and sends the ip addresses and ports to the clients 4 | * @author Nathan Kong, Ardeshir Bastani, Yangcha Ho 5 | * 6 | */ 7 | 8 | import java.net.*; 9 | import java.io.*; 10 | 11 | public class Server2 { 12 | private static String c1IP; 13 | private static int c1Port; 14 | private static String c2IP; 15 | private static int c2Port; 16 | 17 | public static void main(String[] args) throws Exception{ 18 | c1IP = ""; 19 | c1Port = 0; 20 | c2IP = ""; 21 | c2Port = 0; 22 | 23 | //create a socket for udp on port 54545 24 | DatagramSocket serverSocket = new DatagramSocket(54545); 25 | 26 | while(true){ 27 | System.out.println("Waiting for Clients on Port 54545..."); 28 | 29 | //create packet to receive data 30 | DatagramPacket receivePacket = new DatagramPacket(new byte[1024], 1024); 31 | serverSocket.receive(receivePacket); 32 | 33 | //get address and port from datagram 34 | InetAddress ipAddress = receivePacket.getAddress(); 35 | int port = receivePacket.getPort(); 36 | String ip = ipAddress.toString(); 37 | 38 | System.out.println("RECEIVED: " + ip + ":" + port); 39 | } 40 | } 41 | 42 | } 43 | --------------------------------------------------------------------------------