Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.net.*;
- public class PhoneServer
- { // The port number on which the server will be listening
- private static int port = 2014; // The server socket.
- private static ServerSocket listener = null; // The client socket.
- private static Socket clientSocket = null;
- public static void main(String[] args)throws Exception
- {
- boolean listening = true;
- try
- {
- listener = new ServerSocket(port);
- while (listening)
- {
- new ClientThread(listener.accept()).run();
- }
- }
- catch (Exception e)
- {
- System.out.println("I/O failure: " + e.getMessage());
- }
- listener.close();
- /**
- * Open a server socket on the specified port number(2014)
- * and monitor the port for connection requests . When a
- * connection request is received, create a client request
- * thread, passing to its constructor a reference to the
- * Socket object that represents the established connection
- * with the client.
- */
- }
- }
- class ClientThread extends Thread
- {
- Socket socket; //constructor
- public ClientThread(Socket socket)
- {
- this.socket = socket; //Establishes connection
- }
- //implement the run method
- public void run()
- {
- handleConnection(socket);
- }
- //implement the handleConnection method here.
- public void handleConnection(Socket socket) throws IOException
- {
- List<ClientThread> contact = new ArrayList<ClientThread>();
- BufferedReader in = new BufferedReader(new
- InputStreamReader(socket.getInputStream()));
- System.out.print("Received string: '");
- while (!in.ready()) {}
- System.out.println(in.readLine()); // Read one line and output it
- System.out.print("'\n");
- in.close();
- //contact.add(new Contact(23, "Peter"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement