aquaballoon

Java - Socket (Chat App)

Apr 15th, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 KB | None | 0 0
  1. //Server
  2.  
  3. package server;
  4.  
  5. import java.io.DataInputStream;
  6. import java.io.DataOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.net.ServerSocket;
  11. import java.net.Socket;
  12. import java.net.SocketTimeoutException;
  13. import java.util.Scanner;
  14.  
  15. //import java.net.*;
  16. //import java.io.*;
  17. public class Server extends Thread {
  18.  
  19.     private ServerSocket serverSocket;
  20.     private Socket socket;
  21.     private InputStream inFromClient;
  22.     private DataInputStream in;
  23.     private OutputStream outToClient;
  24.     private DataOutputStream out;
  25.  
  26.     public Server(int port) throws IOException {
  27.         //Starting server
  28.         serverSocket = new ServerSocket(port);
  29.         System.out.println("Server Started and listening");
  30.  
  31.         serverSocket.setSoTimeout(60000);   //60 Sec
  32.     }
  33.  
  34.     public void run() {
  35.  
  36.         while (true) {
  37.             try {
  38.  
  39.                 //Accepting from the client
  40.                 socket = serverSocket.accept();
  41.  
  42.                 //Getting the message from the client (read)
  43.                 inFromClient = socket.getInputStream();
  44.                 //in = new DataInputStream(socket.getInputStream());
  45.                 in = new DataInputStream(inFromClient);
  46.                 System.out.println("Client says; " + in.readUTF());
  47.  
  48.                 //Sending the message to the client (write)
  49.                 outToClient = socket.getOutputStream();
  50.                 out = new DataOutputStream(socket.getOutputStream());
  51.                 //out = new DataOutputStream(outToClient);
  52.                 //out.writeUTF("Thank you for connecting to " + socket.getLocalSocketAddress());
  53.  
  54.                 Scanner scn = new Scanner(System.in);
  55.                 String input = scn.nextLine();
  56.                 out.writeUTF(input);
  57.  
  58.                 //Releasing memory
  59.                 socket.close();
  60.  
  61.             } catch (SocketTimeoutException s) {
  62.                 System.out.println("Socket timed out!");
  63.                 break;
  64.             } catch (IOException e) {
  65.                 e.printStackTrace();
  66.                 break;
  67.             }
  68.         }
  69.     }
  70.  
  71.     public static void main(String[] args) {
  72.         //int port = Integer.parseInt(args[0]);
  73.         int port = 8080;
  74.         try {
  75.             Thread t = new Server(port);
  76.             t.start();
  77.         } catch (IOException e) {
  78.             e.printStackTrace();
  79.         }
  80.     }
  81. }
  82.  
  83.  
  84. // Client
  85.  
  86. package client;
  87.  
  88. import java.io.DataInputStream;
  89. import java.io.DataOutputStream;
  90. import java.io.IOException;
  91. import java.io.InputStream;
  92. import java.io.OutputStream;
  93. import java.net.Socket;
  94. import java.util.Scanner;
  95.  
  96. //import java.net.*;
  97. //import java.io.*;
  98. public class Client {
  99.  
  100.     private static Socket socket;
  101.  
  102.     public static void main(String[] args) {
  103. //      String host = args[0];
  104. //      int port = Integer.parseInt(args[1]);
  105.         String host = "localhost";
  106.         int port = 8080;
  107.  
  108.         try {
  109.  
  110.             while (true) {
  111.                 //Connecting to Server
  112.                 socket = new Socket(host, port);
  113.  
  114.                 //Sending the message to the server (write)
  115.                 OutputStream outToServer = socket.getOutputStream();
  116.                 DataOutputStream out = new DataOutputStream(outToServer);
  117.                 //out.writeUTF("Hello from " + socket.getLocalSocketAddress());
  118.  
  119.                 Scanner scn = new Scanner(System.in);
  120.                 String input = scn.nextLine();
  121.                 out.writeUTF(input);
  122.  
  123.                 //Getting the message from the sever (read)
  124.                 InputStream inFromServer = socket.getInputStream();
  125.                 DataInputStream in = new DataInputStream(inFromServer);
  126.                 System.out.println("Server says; " + in.readUTF());
  127.  
  128.                 //Releasing memory
  129.                 socket.close();
  130.             }
  131.  
  132.         } catch (IOException e) {
  133.             e.printStackTrace();
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment