Presentation is loading. Please wait.

Presentation is loading. Please wait.

Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.

Similar presentations


Presentation on theme: "Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings."— Presentation transcript:

1 Socket Programming ENTERPRISE JAVA

2 2 Content  Sockets  Streams  Threads  Readings

3 3 Socket Programming  TCP/IP based network connection  Packet data arrives in order  Delivery of packets is guaranteed  ServerSocket  Socket  InputStream  OutputStream

4 4 Server import java.io.IOException; import java.net.ServerSocket; public class Server { public static void main(String[] args) { ServerSocket server = null; try { server = new ServerSocket(5000); } catch(IOException e) { e.printStackTrace(); } Server Port Required Import Statements

5 5 Server while(true) { try { Socket socket = server.accept(); InputStream is = socket.getInputStream(); StringBuffer mesg = new StringBuffer(); while(true) { int data = is.read(); if(data == -1) { break; } else { mesg.append((char)data); } System.out.println(mesg); } catch(IOException e) { e.printStackTrace(); } Keep serving clients forever Wait for a client to connect Handle client connection with Socket object Get a reference to Socket’s InputStream Read data byte by byte until the end of stream is reached Append data to StringBuffer

6 6 Client import java.io.IOException; import java.io.OutputStream; import java.net.Socket; public class Client { public static void main(String[] args) { try { Socket client = new Socket("localhost",5000); OutputStream os = client.getOutputStream(); os.write("Hello ISCG7425".getBytes()); os.close(); client.close(); } catch(IOException e) { e.printStackTrace(); } Open socket connection to localhost on port 5000 Get a reference to Socket’s Output Stream Use Output Stream’s write(byte[] data) method to send string data to server

7 7 Data Streams  Client and Server must observe the same protocol  Socket Input/Output is data stream oriented  InputStream and OutputStream objects operate on bytes and byte[] arrays  Using the read(byte[] data) and write(byte[] data) methods  Primitive data types need to be converted to and from bytes to be transferred across the network  Problems arise if there is a data type mismatch. All the data types are bytes so can be interpreted as any other data type.

8 8 Data Streams  Helper classes that do all the leg work in converting data to and from bytes  DataInputStream  DataOutputStream  Have methods to read / write a variety of primitive data types  Strings  byte  int  short  float

9 9 Echo Server Socket socket = server.accept(); DataInputStream dis = new DataInputStream(socket.getInputStream()); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); while(true) { try { String mesg = dis.readUTF(); dos.writeUTF("Echo response:"+mesg); } catch(EOFException e) { break; }

10 10 Echo Client Socket client = new Socket("localhost",5000); DataOutputStream dos = new DataOutputStream(client.getOutputStream()); DataInputStream dis = new DataInputStream(client.getInputStream()); dos.writeUTF("Test Message"); String resp = dis.readUTF(); System.out.println(resp); client.close();

11 11 Server Threads  Lots of Socket method calls block waiting for input  accept() – blocks waiting for a client connection  read() – blocks waiting for input  In a server with only one thread of execution – the server is blocked and no over tasks can be processed until the blocking method has finished  Only one client can connect to a single threaded server at a time  Takes a long time to close and open TCP / IP based connections – so can’t rely on close() method

12 12 Server Threads final Socket socket = server.accept(); Thread serverThread = new Thread() { public void run() { try { InputStream is = socket.getInputStream(); StringBuffer mesg = new StringBuffer(); while(true) { int data = is.read(); if(data == -1) break; else mesg.append((char)data); } System.out.println(this.getName() + "\n"+ mesg); }catch(IOException e) { /* … Handle exceptions … */ } } }; serverThread.start();

13 13 Threads  Threads should be used whenever a task includes some sort of long running and blocking input and output  Servers will use one thread per client  Threads allow multiple things to be processed at the same time  Thread.sleep(100) puts the current thread to sleep for 100 ms  Make sure your server loop includes a Thread.sleep() method to give up priority to other threads  All the code executed in a Thread is included in the run() method – a thread is stopped once the run() method finishes

14 14 Readings  http://mindprod.com/jgloss/thread.html http://mindprod.com/jgloss/thread.html  http://docs.oracle.com/javase/tutorial/networking/ sockets/ http://docs.oracle.com/javase/tutorial/networking/ sockets/  http://docs.oracle.com/javase/tutorial/essential/io /index.html http://docs.oracle.com/javase/tutorial/essential/io /index.html


Download ppt "Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings."

Similar presentations


Ads by Google