Presentation is loading. Please wait.

Presentation is loading. Please wait.

CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)

Similar presentations

Presentation on theme: "CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)"— Presentation transcript:

1 CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)

2 Socket Programming Sockets are interfaces that can "plug into" each other over a network. Once so "plugged in", the programs so connected communicate. Network communication can be done by exchanging some data through transmitting that data in a message between a socket in one process and another socket in another process. When messages are sent, the messages are queued at the sending socket until the underlying network protocol has transmitted them. When they arrive, the messages are queued at the receiving socket until the receiving process makes the necessary calls to receive them.

3 Socket Programming An Internet socket is identified by the operating system as a unique combination of the following: Protocol (TCP, UDP or raw IP) Local IP address Local port number Remote IP address (Only for established TCP sockets) Remote port number (Only for established TCP sockets)

4 Socket Programming Client-Server Model A client software process may initiate a communication session, while the server waits for requests from any client. Most business applications being written today use the client-server model. So do the Internet's main application protocols, such as HTTP, SMTP, Telnet, DNS, etc.

5 Socket Programming Client-Server Model

6 Socket Programming Open-Read-Write-Close Before a user process can perform network operations, it Opens a socket to specify and obtain permissions for the network communication. Once a socket is opened, the user process makes one or more calls to Read or Write data through the socket. After all transfer operations are complete, the user process calls Close to inform the operating system that it has finished using that socket.

7 Java Socket - Open Client class Socket This class implements client sockets (also called just "sockets"). public Socket(InetAddress address, int port) throws IOException address - the IP address. port - the port number.

8 Java Socket - Open Socket MyClient; try { MyClient = new Socket("Machine name", PortNumber); } catch (IOException e) { System.out.println(e); }

9 Java Socket – Open Server class ServerSocket This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester. public ServerSocket(int port) throws IOException port - the port number, or 0 to use any free port. public Socket accept() throws IOException Returns: the new Socket

10 Java Socket - Open Open a socket on the server side: ServerSocket MyService; try { MyServerice = new ServerSocket(PortNumber); } catch (IOException e) { System.out.println(e); } Listen for and accept connections from clients on the server side: Socket serviceSocket = null; try { serviceSocket = MyService.accept(); } catch (IOException e) { System.out.println(e); }

11 Java Socket - Read class DataInputStream A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream. public InputStream getInputStream() throws IOException Returns an input stream for this socket. public final String readLine() throws IOException Returns the next line of text from this input stream.

12 Java Socket - Read On the client side: DataInputStream input; try { input = new DataInputStream(MyClient.getInputStream()); } catch (IOException e) { System.out.println(e); } On the server side: DataInputStream input; try { input = new DataInputStream(serviceSocket.getInputStream()); } catch (IOException e) { System.out.println(e); }

13 Java Socket - Write class DataOutputStream A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in. public OutputStream getOutputStream() throws IOException Returns an output stream for writing bytes to this socket. public final void writeBytes(String s) throws IOException s - a string of bytes to be written.

14 Java Socket - Write DataOutputStream output; try { output = new dataOutputStream(MyClient.getOutputStream()); } catch (IOException e) { System.out.println(e); }

15 Java Socket - Close public void close() throws IOException Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created. If this socket has an associated channel then the channel is closed as well.

16 Java Socket - Close On the client side: try { output.close(); input.close(); MyClient.close(); } catch (IOException e) { System.out.println(e); } On the server side: try { output.close(); input.close(); serviceSocket.close(); MyService.close(); } catch (IOException e) {System.out.println(e);}

17 SMTP client – an example

18 Echo server – an example

19 HDB3: High Density Bipolar 3 Encoding: The encoding rules follow those for AMI, except that a sequence of four consecutive 0's are encoding using a special "violation" bit (V). This bit has the same polarity as the last 1-bit which was sent using the AMI encoding rule. When there are even 1-bits between two V bit, a refinement is to encode the four bits as B00V, where B is a balancing pulse. The value of B is assigned as + or -, which has the opposite polarity as the last 1-bit.

20 HDB3: High Density Bipolar 3 Encoding Message: 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 1 1 AMI:+1 0 0 0 0 -1 0 0 0 0 +1 -1 0 0 0 0 +1 -1 HDB3: +1 0 0 0 +V -1 0 0 0 -V +1 -1 +B 0 0 +V -1 +1

21 HDB3: High Density Bipolar 3 Decoding: Finds all V bits; then converts “B00V” and “000V” to “0000”. Converts -1 to 1.

22 Lab 4 The input to the encoding program is a string of 0 and 1 from the keyboard. The input binary string will be encoded by the encoding program to the HDB3 stream. The encoding program should send a “request-to-send” message to the decoding program and waiting for a “clear- to-send” message from the decoding program, before sending. The decoding program will send a “clear-to-send” message to the encoding program after receiving a “request-to-send” message from the encoding program.

23 Lab 4 The HDB3 stream will be transmitted to the decoding program through socket. After received the HDB3 stream, the decoding program will acknowledge the receipt to the encoding program. Then the decoding program will decode the HDB3 stream into its original format and print on the screen. The HDB3 stream is represented by the sequence of three characters, “+”, “-” and “0”, respectively meaning the positive pulse, negative pulse, and no-line-signal.

24 Lab 4 You should try to use two computers to simulate the sender and receiver You should try to see whether other group’s receiving program can understand what you sent You should also try to see whether your receiving program can understand what other group sent.

Download ppt "CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)"

Similar presentations

Ads by Google