704b68c724afbf7dd32efa891070dffc.ppt
- Количество слайдов: 44
Client / Server in Java
Basic Sockets Classes
Socket Client/Server in Java
TCPecho / TCPechos
Iterative TCP echo Server package tcpechos; import java. net. *; import java. io. *; public class TCPechos { public static void main(String[] args) { Socket sock; Server. Socket ssock; Input. Stream is. In; Print. Stream ps. Out; int i. Num. Read, num. Port; String send. Msg, port = "5678"; byte ab. In[] = new byte[1024];
Iterative TCP echo Server // Handle command line arguments switch (args. length) { case 1: port = args[0]; break; case 0: break; default: System. out. println("Illegal number of arguments. "); System. exit(1); } num. Port = Integer. parse. Int(port); //port number must be an int for Socket System. out. println ("Serv: Initializing to port " + num. Port);
Iterative TCP echo Server try { // Create the server socket and bind it to our port ssock = new Server. Socket (num. Port); while (true) { // Wait for a client request System. out. println ("Serv: Waiting for a connection. . . "); sock = ssock. accept(); // We got a connection request! Print out info and start talking System. out. println ("Serv: Receved a connection"); System. out. println ("*--------*"); System. out. println ("Srx: Connected to: " +sock. get. Inet. Address() + ": " +sock. get. Port()); ps. Out = new Print. Stream (sock. get. Output. Stream()); is. In = sock. get. Input. Stream(); i. Num. Read = 0;
Iterative TCP echo Server while (i. Num. Read >= 0) { //Keep reading as long as we get data i. Num. Read = is. In. read(ab. In, 0, 1024); if (i. Num. Read < 0) break; send. Msg = new String(ab. In, 0, i. Num. Read); System. out. println("We just received: (" +i. Num. Read + ") " + send. Msg); //Note that the received message includes 'n' and //we add one of our own with println ps. Out. print(send. Msg); i. Num. Read = 0; } sock. close(); System. out. println ("Srvx disconnected from " + sock. get. Inet. Address()); System. out. println ("*-----------*"); } //end of while } //end of try catch (Exception e) { System. err. println (": Serv: Exception in main: " + e); } } //end of main() } //end of TCPechos
Basic TCP echo Client import java. io. *; import java. util. *; public class Basic. TCPecho { public static void main(String[] args) { // TODO code application logic here Socket sock; String s. In, host = "localhost", port = "5678"; //default host and port Input. Stream is. In; Print. Stream ps. Out; byte ab. In[] = new byte[1024]; int i. Num. Read, num. Port; Scanner input;
Basic TCP echo Client
Basic TCP echo Client try { sock = new Socket(host, num. Port); is. In = sock. get. Input. Stream(); ps. Out = new Print. Stream(sock. get. Output. Stream()); input = new Scanner (System. in); while (true) { //First we get some text from the user. System. out. print("Enter text to read: "); s. In = input. next. Line(); //If we typed “bye”, then exit client if (s. In. equals("bye")) { sock. close(); break; }
Basic TCP echo Client //next we send that to the server ps. Out. print(s. In); //Now, wait for something to come back. . . i. Num. Read = is. In. read(ab. In, 0, 1024); if (i. Num. Read < 0) { break; //connection has been closed } String str = new String(ab. In, 0, i. Num. Read); System. out. println("Client: Rcvd from server: " + str + " (" + i. Num. Read + " bytes)"); } sock. close(); } catch (Exception e) { System. out. println("Client: exception " + e); } }//end of main }//end of class
Threads in Java
Thread State Diagram
Thread Priorities
Thread class
Implementing Threads (1)
Using Threads (1)
Implementing Threads (2)
Using extended classes
Multi-threaded server Output
Multi-threaded TCPecho Server
Multi-threaded TCPecho Server
Multi-threaded TCPecho Server
Multi-threaded TCPecho Server
Multi-threaded TCPecho Server
Net. Beans Graphical Client
Net. Beans Project
Net. Beans Project Structure
Net. Beans Project Layout
UDP Temperature Server
UDP Temperature Server
UDP Temperature Server
UDP Temp Client - constructor
UDP Temp Client - constructor
UDP Temp Client action. Performed
UDP Temp Client action. Performed
UDP Temp Client – window methods
UDP Temp Client – private data
Summary
704b68c724afbf7dd32efa891070dffc.ppt