public void startServer() throws Exception { while (true) { Socket s = server.accept(); cClient.add(new ClientConn(s)); ta.append("NEW-CLIENT " + s.getInetAddress() + ":" + s.getPort()); ta.append("\n" + "CLIENTS-COUNT: " + cClient.size() + "\n\n"); } }
public Server(int port) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port); clientSocket = serverSocket.accept(); chatField.setText("------CONNECTION ESTABLISHED------"); out = new PrintWriter(clientSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); enableChat(); } catch (IOException e) { chatField.setText("Connection Fail"); } }
public void run() { Socket socket = null; try { host = InetAddress.getLocalHost().getHostAddress(); l.setText("Adres servera : " + host); serverSocket = new ServerSocket(port); JOptionPane.showMessageDialog(null, "Serwer dzia³a na adresie " + host); while (true) { socket = serverSocket.accept(); if (socket != null) { sList.add(new ServerThread(socket, clientList, this)); } } } catch (Exception e) { } }
public ReciveFile() { super("파일전송"); setLayout(null); lbl = new Label("파일 전송을 기다립니다."); lbl.setBounds(10, 30, 230, 20); lbl.setBackground(Color.gray); lbl.setForeground(Color.white); add(lbl); txt = new TextArea("", 0, 0, TextArea.SCROLLBARS_BOTH); txt.setBounds(10, 60, 230, 100); txt.setEditable(false); add(txt); btn = new Button("닫기"); btn.setBounds(105, 170, 40, 20); btn.setVisible(false); btn.addActionListener(this); add(btn); addWindowListener(new WinListener()); setSize(250, 200); show(); try { ServerSocket socket = new ServerSocket(port); Socket sock = null; FileThread client = null; try { sock = socket.accept(); client = new FileThread(this, sock); client.start(); } catch (IOException e) { System.out.println(e); try { if (sock != null) sock.close(); } catch (IOException e1) { System.out.println(e1); } finally { sock = null; } } } catch (IOException e) { } }
@Override public void run() { try { // Create a server socket ServerSocket serverSocket = new ServerSocket(8000); textArea.append("TCP connection listener started at " + new Date() + '\n'); while (true) { // Listen for a new connection request Socket socket = serverSocket.accept(); // Display the client number textArea.append( "Starting thread for TCP client " + clientNo + " at " + new Date() + '\n'); // Find the client's host name, and IP address InetAddress inetAddress = socket.getInetAddress(); textArea.append( "Client " + clientNo + "'s host name is " + inetAddress.getHostName() + "\n"); textArea.append( "Client " + clientNo + "'s IP Address is " + inetAddress.getHostAddress() + "\n"); // Create a new thread for the TCP connection HandleTCPClient task = new HandleTCPClient(socket); // Start the new thread new Thread(task).start(); // Increment clientNo clientNo++; } } catch (IOException ex) { System.err.println(ex); } }
// The main procedure public static void main(String args[]) { String s; initGUI(); while (true) { try { // Poll every ~10 ms Thread.sleep(10); } catch (InterruptedException e) { } switch (connectionStatus) { case BEGIN_CONNECT: try { // Try to set up a server if host if (isHost) { hostServer = new ServerSocket(port); socket = hostServer.accept(); } // If guest, try to connect to the server else { socket = new Socket(hostIP, port); } in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); changeStatusTS(CONNECTED, true); } // If error, clean up and output an error message catch (IOException e) { cleanUp(); changeStatusTS(DISCONNECTED, false); } break; case CONNECTED: try { // Send data if (toSend.length() != 0) { out.print(toSend); out.flush(); toSend.setLength(0); changeStatusTS(NULL, true); } // Receive data if (in.ready()) { s = in.readLine(); if ((s != null) && (s.length() != 0)) { // Check if it is the end of a trasmission if (s.equals(END_CHAT_SESSION)) { changeStatusTS(DISCONNECTING, true); } // Otherwise, receive what text else { appendToChatBox("INCOMING: " + s + "\n"); changeStatusTS(NULL, true); } } } } catch (IOException e) { cleanUp(); changeStatusTS(DISCONNECTED, false); } break; case DISCONNECTING: // Tell other chatter to disconnect as well out.print(END_CHAT_SESSION); out.flush(); // Clean up (close all streams/sockets) cleanUp(); changeStatusTS(DISCONNECTED, true); break; default: break; // do nothing } } }
// ------------------------------------ // main // ------------------------------------ public static void main(String argv[]) throws Exception { // create a Server object Server theServer = new Server(); // show GUI: theServer.pack(); theServer.setVisible(true); // get RTSP socket port from the command line int RTSPport = Integer.parseInt(argv[0]); // Initiate TCP connection with the client for the RTSP session ServerSocket listenSocket = new ServerSocket(RTSPport); theServer.RTSPsocket = listenSocket.accept(); listenSocket.close(); // Get Client IP address theServer.ClientIPAddr = theServer.RTSPsocket.getInetAddress(); // Initiate RTSPstate state = INIT; // Set input and output stream filters: RTSPBufferedReader = new BufferedReader(new InputStreamReader(theServer.RTSPsocket.getInputStream())); RTSPBufferedWriter = new BufferedWriter(new OutputStreamWriter(theServer.RTSPsocket.getOutputStream())); // Wait for the SETUP message from the client int request_type; boolean done = false; while (!done) { request_type = theServer.parse_RTSP_request(); // blocking if (request_type == SETUP) { done = true; // update RTSP state state = READY; System.out.println("New RTSP state: READY"); // Send response theServer.send_RTSP_response(); // init the VideoStream object: theServer.video = new VideoStream(VideoFileName); // init RTP socket theServer.RTPsocket = new DatagramSocket(); } } // loop to handle RTSP requests while (true) { // parse the request request_type = theServer.parse_RTSP_request(); // blocking if ((request_type == PLAY) && (state == READY)) { // send back response theServer.send_RTSP_response(); // start timer theServer.timer.start(); // update state state = PLAYING; System.out.println("New RTSP state: PLAYING"); } else if ((request_type == PAUSE) && (state == PLAYING)) { // send back response theServer.send_RTSP_response(); // stop timer theServer.timer.stop(); // update state state = READY; System.out.println("New RTSP state: READY"); } else if (request_type == TEARDOWN) { // send back response theServer.send_RTSP_response(); // stop timer theServer.timer.stop(); // close sockets theServer.RTSPsocket.close(); theServer.RTPsocket.close(); System.exit(0); } } }
// wait for connection, then display connection information public void waitForConnection() throws IOException { showMessage("Waiting for someone to connect... \n"); connection = server.accept(); showMessage("Now connected to " + connection.getInetAddress().getHostName()); }