ceg3185 tutorial 4 prepared by zhenxia zhang [email protected] revised by jiying zhao (2015w)

24
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang [email protected] Revised by Jiying Zhao (2015w)

Upload: william-jacobs

Post on 24-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

CEG3185Tutorial 4

Prepared by Zhenxia [email protected]

Revised by Jiying Zhao (2015w)

Page 2: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Socket ProgrammingSockets 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.

Page 3: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Socket ProgrammingAn Internet socket is identified by the

operating system as a unique combination of the following:Protocol (TCP, UDP or raw IP)Local IP addressLocal port numberRemote IP address (Only for established TCP

sockets)Remote port number (Only for established TCP

sockets)

Page 4: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Socket ProgrammingClient-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.

Page 5: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Socket ProgrammingClient-Server Model

Page 6: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Socket ProgrammingOpen-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.

Page 7: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Java Socket - OpenClient

class Socket This class implements client sockets (also called

just "sockets").public Socket(InetAddress address, int port)

throws IOExceptionaddress - the IP address.port - the port number.

Page 8: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Java Socket - OpenSocket MyClient;

try {MyClient = new Socket("Machine

name", PortNumber);}catch (IOException e) {

System.out.println(e);}

Page 9: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Java Socket – OpenServer

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 IOExceptionReturns: the new Socket

Page 10: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

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);}

Page 11: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Java Socket - Readclass 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.

Page 12: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

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);}

Page 13: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Java Socket - Writeclass 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.

Page 14: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Java Socket - WriteDataOutputStream output;try {

output = new dataOutputStream(MyClient.getOutputStream());}catch (IOException e) {

System.out.println(e);}

Page 15: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Java Socket - Closepublic void close()

throws IOExceptionOnce 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.

Page 16: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

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);}

Page 17: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

SMTP client – an example

Page 18: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Echo server – an example

Page 19: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

HDB3: High Density Bipolar 3Encoding:

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.

Page 20: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

HDB3: High Density Bipolar 3Encoding

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

Page 21: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

HDB3: High Density Bipolar 3Decoding:

Finds all V bits; then converts “B00V” and “000V” to “0000”.

Converts -1 to 1.

Page 22: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Lab 4The 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.

Page 23: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Lab 4The 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.

Page 24: CEG3185 Tutorial 4 Prepared by Zhenxia Zhang zzhan036@uottawa.ca Revised by Jiying Zhao (2015w)

Lab 4You should try to use two computers to

simulate the sender and receiverYou 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.