├── ChatApplication ├── ChatSocketClient$1.class ├── ChatSocketClient$2.class ├── ChatSocketClient.class ├── ChatSocketClient.java ├── ChatSocketServer$1.class ├── ChatSocketServer$2.class ├── ChatSocketServer.class └── ChatSocketServer.java ├── FileTransfer ├── Client │ ├── client.class │ ├── client.java │ └── kelta_king.txt └── Server │ ├── server.class │ └── server.java ├── Images └── ChatAPP │ └── ChatView.PNG ├── README.md ├── TCP Protocol ├── Client │ ├── client.class │ └── client.java └── Server │ ├── server.class │ └── server.java └── UDP Protocol ├── Client ├── client.class └── client.java └── Server ├── server.class └── server.java /ChatApplication/ChatSocketClient$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/ChatApplication/ChatSocketClient$1.class -------------------------------------------------------------------------------- /ChatApplication/ChatSocketClient$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/ChatApplication/ChatSocketClient$2.class -------------------------------------------------------------------------------- /ChatApplication/ChatSocketClient.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/ChatApplication/ChatSocketClient.class -------------------------------------------------------------------------------- /ChatApplication/ChatSocketClient.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.Socket; 3 | import java.net.SocketException; 4 | import java.net.UnknownHostException; 5 | 6 | 7 | 8 | public class ChatSocketClient { 9 | private Socket socket = null; 10 | private InputStream inStream = null; 11 | private OutputStream outStream = null; 12 | 13 | public ChatSocketClient() { 14 | 15 | } 16 | 17 | public void createSocket() { 18 | try { 19 | socket = new Socket("localHost", 3339); 20 | System.out.println("Connected"); 21 | inStream = socket.getInputStream(); 22 | outStream = socket.getOutputStream(); 23 | createReadThread(); 24 | createWriteThread(); 25 | } catch (UnknownHostException u) { 26 | u.printStackTrace(); 27 | } catch (IOException io) { 28 | io.printStackTrace(); 29 | } 30 | } 31 | 32 | public void createReadThread() { 33 | Thread readThread = new Thread() { 34 | public void run() { 35 | while (socket.isConnected()) { 36 | 37 | try { 38 | byte[] readBuffer = new byte[200]; 39 | int num = inStream.read(readBuffer); 40 | 41 | if (num > 0) { 42 | byte[] arrayBytes = new byte[num]; 43 | System.arraycopy(readBuffer, 0, arrayBytes, 0, num); 44 | String recvedMessage = new String(arrayBytes, "UTF-8"); 45 | System.out.println("Received message :" + recvedMessage); 46 | }/* else { 47 | // notify(); 48 | }*/ 49 | ; 50 | //System.arraycopy(); 51 | }catch (SocketException se){ 52 | System.exit(0); 53 | 54 | } catch (IOException i) { 55 | i.printStackTrace(); 56 | } 57 | 58 | } 59 | } 60 | }; 61 | readThread.setPriority(Thread.MAX_PRIORITY); 62 | readThread.start(); 63 | } 64 | 65 | public void createWriteThread() { 66 | Thread writeThread = new Thread() { 67 | public void run() { 68 | while (socket.isConnected()) { 69 | 70 | try { 71 | BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); 72 | sleep(100); 73 | String typedMessage = inputReader.readLine(); 74 | if (typedMessage != null && typedMessage.length() > 0) { 75 | synchronized (socket) { 76 | outStream.write(typedMessage.getBytes("UTF-8")); 77 | sleep(100); 78 | } 79 | } 80 | ; 81 | //System.arraycopy(); 82 | 83 | } catch (IOException i) { 84 | i.printStackTrace(); 85 | } catch (InterruptedException ie) { 86 | ie.printStackTrace(); 87 | } 88 | 89 | } 90 | } 91 | }; 92 | writeThread.setPriority(Thread.MAX_PRIORITY); 93 | writeThread.start(); 94 | } 95 | 96 | public static void main(String[] args) throws Exception { 97 | ChatSocketClient myChatClient = new ChatSocketClient(); 98 | myChatClient.createSocket(); 99 | /*myChatClient.createReadThread(); 100 | myChatClient.createWriteThread();*/ 101 | } 102 | } -------------------------------------------------------------------------------- /ChatApplication/ChatSocketServer$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/ChatApplication/ChatSocketServer$1.class -------------------------------------------------------------------------------- /ChatApplication/ChatSocketServer$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/ChatApplication/ChatSocketServer$2.class -------------------------------------------------------------------------------- /ChatApplication/ChatSocketServer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/ChatApplication/ChatSocketServer.class -------------------------------------------------------------------------------- /ChatApplication/ChatSocketServer.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.ServerSocket; 3 | import java.net.Socket; 4 | import java.net.SocketException; 5 | 6 | 7 | 8 | public class ChatSocketServer { 9 | private ServerSocket severSocket = null; 10 | private Socket socket = null; 11 | private InputStream inStream = null; 12 | private OutputStream outStream = null; 13 | 14 | public ChatSocketServer() { 15 | 16 | } 17 | 18 | public void createSocket() { 19 | try { 20 | ServerSocket serverSocket = new ServerSocket(3339); 21 | while (true) { 22 | socket = serverSocket.accept(); 23 | inStream = socket.getInputStream(); 24 | outStream = socket.getOutputStream(); 25 | System.out.println("Connected"); 26 | createReadThread(); 27 | createWriteThread(); 28 | 29 | } 30 | } catch (IOException io) { 31 | io.printStackTrace(); 32 | } 33 | } 34 | 35 | public void createReadThread() { 36 | Thread readThread = new Thread() { 37 | public void run() { 38 | while (socket.isConnected()) { 39 | try { 40 | byte[] readBuffer = new byte[200]; 41 | int num = inStream.read(readBuffer); 42 | if (num > 0) { 43 | byte[] arrayBytes = new byte[num]; 44 | System.arraycopy(readBuffer, 0, arrayBytes, 0, num); 45 | String recvedMessage = new String(arrayBytes, "UTF-8"); 46 | System.out.println("Received message :" + recvedMessage); 47 | } else { 48 | notify(); 49 | } 50 | ; 51 | //System.arraycopy(); 52 | 53 | } catch (SocketException se) { 54 | System.exit(0); 55 | 56 | } catch (IOException i) { 57 | i.printStackTrace(); 58 | } 59 | 60 | } 61 | } 62 | }; 63 | readThread.setPriority(Thread.MAX_PRIORITY); 64 | readThread.start(); 65 | } 66 | 67 | public void createWriteThread() { 68 | Thread writeThread = new Thread() { 69 | public void run() { 70 | 71 | while (socket.isConnected()) { 72 | try { 73 | BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); 74 | sleep(100); 75 | String typedMessage = inputReader.readLine(); 76 | if (typedMessage != null && typedMessage.length() > 0) { 77 | synchronized (socket) { 78 | outStream.write(typedMessage.getBytes("UTF-8")); 79 | sleep(100); 80 | } 81 | }/* else { 82 | notify(); 83 | }*/ 84 | ; 85 | //System.arraycopy(); 86 | 87 | } catch (IOException i) { 88 | i.printStackTrace(); 89 | } catch (InterruptedException ie) { 90 | ie.printStackTrace(); 91 | } 92 | 93 | } 94 | } 95 | }; 96 | writeThread.setPriority(Thread.MAX_PRIORITY); 97 | writeThread.start(); 98 | 99 | } 100 | 101 | public static void main(String[] args) { 102 | ChatSocketServer chatServer = new ChatSocketServer(); 103 | chatServer.createSocket(); 104 | 105 | } 106 | } -------------------------------------------------------------------------------- /FileTransfer/Client/client.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/FileTransfer/Client/client.class -------------------------------------------------------------------------------- /FileTransfer/Client/client.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.Socket; 3 | 4 | public class client { 5 | public static void main(String[] args) throws Exception { 6 | 7 | int filesize=1600; 8 | int bytesRead; 9 | int currentTot = 0; 10 | 11 | Socket socket = new Socket("192.168.43.50",36065); 12 | byte [] bytearray = new byte [filesize]; 13 | InputStream is = socket.getInputStream(); 14 | 15 | FileOutputStream fos = new FileOutputStream("E:\\JAVA_Programs/FTP/Client/kelta_king.txt"); 16 | BufferedOutputStream bos = new BufferedOutputStream(fos); 17 | bytesRead = is.read(bytearray,0,bytearray.length); 18 | currentTot = bytesRead; 19 | 20 | do { 21 | 22 | bytesRead = is.read(bytearray, currentTot, (bytearray.length-currentTot)); 23 | if(bytesRead >= 0){ 24 | currentTot += bytesRead; 25 | } 26 | 27 | } while(bytesRead > -1); 28 | 29 | bos.write(bytearray, 0 , currentTot); 30 | bos.flush(); 31 | bos.close(); 32 | socket.close(); 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /FileTransfer/Client/kelta_king.txt: -------------------------------------------------------------------------------- 1 | this is server file -------------------------------------------------------------------------------- /FileTransfer/Server/server.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/FileTransfer/Server/server.class -------------------------------------------------------------------------------- /FileTransfer/Server/server.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.Socket; 3 | import java.net.ServerSocket; 4 | 5 | public class server { 6 | public static void main (String[] args) throws Exception 7 | { 8 | ServerSocket serverSocket = new ServerSocket(36065); 9 | Socket socket = serverSocket.accept(); 10 | System.out.println("Accepted connection : " + socket); 11 | File transferFile = new File ("E:\\JAVA_Programs/FTP/Server/kelta.txt"); 12 | 13 | byte [] b = new byte [(int)transferFile.length()]; 14 | FileInputStream fin = new FileInputStream(transferFile); 15 | BufferedInputStream bin = new BufferedInputStream(fin); 16 | 17 | bin.read(b,0,b.length); 18 | OutputStream os = socket.getOutputStream(); 19 | System.out.println("Sending Files..."); 20 | os.write(b,0,b.length); 21 | os.flush(); 22 | socket.close(); 23 | System.out.println("File has been transferred"); 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Images/ChatAPP/ChatView.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/Images/ChatAPP/ChatView.PNG -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java_Socket_programming 2 | This repo contains the codes for Socket programming in JAVA. Sockets allow communication between two different processes on the same or different machines and Socket programming is a way of connecting two nodes on a network to communicate with each other. 3 | 4 | There are 4 sections in this repo. 5 | - TCP Protocol implementation 6 | - UDP Protocol implementation 7 | - File Transfer Protocol imple. 8 | - A client and server ChatApplication 9 | 10 | **1) TCP Protocol implementation** 11 | 12 | TCP provides a connection oriented service, since it is based on connections between clients and servers. TCP provides reliability. When a TCP client send data to the server, it requires an acknowledgement in return. 13 | 14 | **2) UDP Protocol implementation** 15 | 16 | UDP is a simple transport-layer protocol. The application writes a message to a UDP socket, which is then encapsulated in a UDP datagram, which is further encapsulated in an IP datagram, which is sent to the destination. 17 | 18 | **3) File Transfer Protocol imple.** 19 | 20 | FTP is a standard internet protocol provided by TCP/IP used for transmitting the files from one host to another. It is mainly used for transferring the web page files from their creator to the computer that acts as a server for other computers on the internet. 21 | 22 | **4) A client and server ChatApplication** 23 | 24 | In this application client and server sends, reads and replies the messages to each other. It is a cmd based chat application. Below is the image of the chat application view. 25 | 26 | ![](https://github.com/Kelta-King/Java_Socket_programming/blob/main/Images/ChatAPP/ChatView.PNG) 27 | -------------------------------------------------------------------------------- /TCP Protocol/Client/client.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/TCP Protocol/Client/client.class -------------------------------------------------------------------------------- /TCP Protocol/Client/client.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.Socket; 3 | public class client { 4 | 5 | public static void main(String[] args) throws IOException { 6 | 7 | System.out.println("client at port 6666"); 8 | String ip = "localhost"; 9 | int port = 6666; 10 | Socket skt = new Socket(ip,port); 11 | 12 | OutputStreamWriter osw = new OutputStreamWriter(skt.getOutputStream()); 13 | PrintWriter out = new PrintWriter(osw); 14 | 15 | out.println("1"); 16 | out.flush(); 17 | 18 | BufferedReader br = new BufferedReader(new InputStreamReader(skt.getInputStream())); 19 | String rslt; 20 | 21 | while ((rslt = br.readLine())!= null) { 22 | System.out.println(rslt); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /TCP Protocol/Server/server.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/TCP Protocol/Server/server.class -------------------------------------------------------------------------------- /TCP Protocol/Server/server.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.ServerSocket; 3 | import java.net.Socket; 4 | 5 | public class server { 6 | 7 | public static void main(String[] args) throws IOException{ 8 | 9 | System.out.println("server and port 6666"); 10 | ServerSocket sskt = new ServerSocket(6666); 11 | Socket skt = sskt.accept(); 12 | 13 | BufferedReader br= new BufferedReader(new InputStreamReader(skt.getInputStream())); 14 | String str = br.readLine(); 15 | 16 | OutputStreamWriter osw = new OutputStreamWriter(skt.getOutputStream()); 17 | PrintWriter out = new PrintWriter(osw); 18 | 19 | if (Integer.parseInt(str) == 0) { 20 | 21 | Process p = Runtime.getRuntime().exec("cmd /c dir"); 22 | BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); 23 | r.readLine(); 24 | String line; 25 | 26 | while ((line = r.readLine()) != null) { 27 | out.println(line); 28 | out.flush(); 29 | } 30 | 31 | } 32 | else { 33 | 34 | Process p = Runtime.getRuntime().exec("cmd /c date /t"); 35 | System.out.println("date "); 36 | BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); 37 | String line; 38 | 39 | while ((line = r.readLine()) != null) { 40 | out.println(line); 41 | out.flush(); 42 | } 43 | 44 | } 45 | 46 | skt.shutdownOutput(); 47 | sskt.close(); 48 | 49 | } 50 | } -------------------------------------------------------------------------------- /UDP Protocol/Client/client.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/UDP Protocol/Client/client.class -------------------------------------------------------------------------------- /UDP Protocol/Client/client.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.DatagramPacket; 3 | import java.net.DatagramSocket; 4 | import java.net.InetAddress; 5 | import java.net.SocketException; 6 | 7 | public class client { 8 | 9 | public static void main(String[] args) throws IOException { 10 | 11 | DatagramSocket ds = new DatagramSocket(); 12 | 13 | byte[] bt1 = String.valueOf(0).getBytes(); 14 | //here this i can contain values of 0 or 1 15 | InetAddress ia = InetAddress.getLocalHost(); 16 | 17 | int port = 6666; 18 | 19 | DatagramPacket dp1 = new DatagramPacket(bt1,bt1.length,ia,port); 20 | ds.send(dp1); 21 | Boolean yo = true; 22 | int i = 0; 23 | 24 | while(true){ 25 | 26 | byte[] bt2 = new byte[1024]; 27 | DatagramPacket dp2 = new DatagramPacket(bt2, bt2.length); 28 | ds.receive(dp2); 29 | String output = new String(dp2.getData(), 0, dp2.getLength()); 30 | System.out.println(output); 31 | 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /UDP Protocol/Server/server.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kelta-King/Java_Socket_programming/39f878703d8f5e44fa01353302c1c4691647a9e2/UDP Protocol/Server/server.class -------------------------------------------------------------------------------- /UDP Protocol/Server/server.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.DatagramPacket; 3 | import java.net.DatagramSocket; 4 | import java.net.InetAddress; 5 | import java.net.SocketException; 6 | 7 | public class server { 8 | 9 | public static void main(String[] args) throws IOException { 10 | 11 | DatagramSocket ds = new DatagramSocket(6666); 12 | 13 | System.out.println("Server is listing on port 6666"); 14 | 15 | byte[] bt1 = new byte[1024]; 16 | DatagramPacket dp1 = new DatagramPacket(bt1,bt1.length); 17 | ds.receive(dp1); 18 | 19 | int num = Integer.parseInt(new String(dp1.getData(),0,dp1.getLength()).trim()); 20 | System.out.println(num); 21 | 22 | if (num == 0) { 23 | Process p = Runtime.getRuntime().exec("cmd /c dir"); 24 | BufferedReader reader = new BufferedReader(new 25 | InputStreamReader(p.getInputStream())); 26 | String line=reader.readLine(); 27 | 28 | while (line!=null) { 29 | 30 | byte[] bt2 = (line+"").getBytes(); 31 | InetAddress ia = InetAddress.getLocalHost(); 32 | DatagramPacket dp2 = new DatagramPacket(bt2,bt2.length,ia,dp1.getPort()); 33 | ds.send(dp2); 34 | line = reader.readLine(); 35 | 36 | } 37 | } 38 | else{ 39 | 40 | Process p = Runtime.getRuntime().exec("cmd /c date /t"); 41 | BufferedReader reader = new BufferedReader(new 42 | InputStreamReader(p.getInputStream())); 43 | String line; 44 | while ((line = reader.readLine())!=null) { 45 | 46 | byte[] b2 = (line+"").getBytes(); 47 | InetAddress ia = InetAddress.getLocalHost(); 48 | DatagramPacket dp2 = new DatagramPacket(b2,b2.length,ia,dp1.getPort()); 49 | ds.send(dp2); 50 | line=reader.readLine(); 51 | 52 | } 53 | } 54 | } 55 | } 56 | --------------------------------------------------------------------------------