socket programming in java -first step of network programming-

22
Socket Socket Programming in Programming in Java Java -First Step of Network -First Step of Network Programming- Programming-

Upload: barrie-morgan

Post on 25-Dec-2015

264 views

Category:

Documents


0 download

TRANSCRIPT

Socket Socket Programming in Programming in

JavaJava-First Step of Network -First Step of Network

Programming-Programming-

How to Open a Socket?How to Open a Socket?-Client--Client-

Socket MyClient; Socket MyClient;

MyClient = new Socket("Machine MyClient = new Socket("Machine name", PortNumber); name", PortNumber);

How to Open a Socket?How to Open a Socket?-Client--Client-

Machine name is the machine you are Machine name is the machine you are trying to open a connection to(ex: ip trying to open a connection to(ex: ip address or workstation name), and address or workstation name), and PortNumber is the port (a number) on PortNumber is the port (a number) on which the server you are trying to which the server you are trying to connect to is running. connect to is running.

When selecting a port number, you When selecting a port number, you should note that port numbers between should note that port numbers between 0 and 1,023 are reserved for privileged 0 and 1,023 are reserved for privileged users (that is, super user or root). users (that is, super user or root).

How to Open a Socket?How to Open a Socket?-Client--Client-

These port numbers are reserved for These port numbers are reserved for standard services, such as email, standard services, such as email, FTP, and HTTP. FTP, and HTTP.

When selecting a port number for When selecting a port number for your server, select one that is your server, select one that is greater than 1,023! greater than 1,023!

How to Open a Socket?How to Open a Socket?-Client--Client-

With exception handling, the code look like With exception handling, the code look like following:following:

Socket MyClient; Socket MyClient; try { try { MyClient = new Socket("Machine name", MyClient = new Socket("Machine name", PortNumber); PortNumber);

} } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); } }

How to Open a Socket?How to Open a Socket?-Server--Server-

ServerSocket MyService; ServerSocket MyService;

try { try { MyServerice = new MyServerice = new ServerSocket(PortNumber); ServerSocket(PortNumber);

}}

catch (IOException e) { catch (IOException e) {

System.out.println(e); System.out.println(e);

} }

How to Open a Socket?How to Open a Socket?-Server--Server-

When implementing a server you also need to When implementing a server you also need to create a socket object from the ServerSocket in create a socket object from the ServerSocket in order to listen for and accept connections from order to listen for and accept connections from clients. clients.

Socket clientSocket = null; Socket clientSocket = null; try { try {

serviceSocket = MyService.accept();serviceSocket = MyService.accept(); } } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); } }

How Do I Create an Input How Do I Create an Input Stream?Stream?-Client--Client-

On the client side, you can use the On the client side, you can use the DataInputStream class to create an input stream DataInputStream class to create an input stream to receive response from the server: to receive response from the server:

DataInputStream input; DataInputStream input;

try { try {

input = new input = new DataInputStream(MyClient.getInputStream()); DataInputStream(MyClient.getInputStream());

} }

catch (IOException e) { catch (IOException e) {

System.out.println(e); System.out.println(e);

} }

How Do I Create an Input How Do I Create an Input Stream?Stream?-Client--Client-

The class DataInputStream allows you to The class DataInputStream allows you to read lines of text and Java primitive data read lines of text and Java primitive data types in a portable way. types in a portable way.

It has methods such as read, readChar, It has methods such as read, readChar, readInt, readDouble, and readLine,. readInt, readDouble, and readLine,.

Use whichever function you think suits Use whichever function you think suits your needs depending on the type of data your needs depending on the type of data that you receive from the server. that you receive from the server.

How Do I Create an Input How Do I Create an Input Stream?Stream?-Server--Server-

On the server side, you can use DataInputStream On the server side, you can use DataInputStream to receive input from the client to receive input from the client

DataInputStream input; DataInputStream input;

try { try {

input = new input = new DataInputStream(serviceSocket.getInputStream());DataInputStream(serviceSocket.getInputStream());

} }

catch (IOException e) {catch (IOException e) {

System.out.println(e); System.out.println(e);

} }

How do I Create an Output How do I Create an Output Stream?Stream?-Client--Client-

On the client side, you can create an On the client side, you can create an output stream to send information to output stream to send information to the server socket using the class the server socket using the class PrintStream or DataOutputStream of PrintStream or DataOutputStream of java.io: java.io:

How do I Create an Output How do I Create an Output Stream?Stream?-Client--Client-

PrintStream output; PrintStream output;

try { try {

output = new output = new

PrintStream(MyClient.getOutputStream());PrintStream(MyClient.getOutputStream()); } }

catch (IOException e) { catch (IOException e) {

System.out.println(e); System.out.println(e);

} }

How do I Create an Output How do I Create an Output Stream?Stream?-Client--Client-

The class PrintStream has methods The class PrintStream has methods for displaying textual representation for displaying textual representation of Java primitive data types. of Java primitive data types.

you may use the DataOutputStream you may use the DataOutputStream

How do I Create an Output How do I Create an Output Stream?Stream?-Client--Client-

DataOutputStream output; DataOutputStream output;

try { try { output = new output = new

DataOutputStream(MyClient.getOutputStream()); DataOutputStream(MyClient.getOutputStream());

} }

catch (IOException e) {catch (IOException e) {

System.out.println(e); System.out.println(e);

} }

How do I Create an Output How do I Create an Output Stream?Stream?-Client--Client-

The class DataOutputStream allows The class DataOutputStream allows you to write Java primitive data you to write Java primitive data types; many of its methods write a types; many of its methods write a single Java primitive type to the single Java primitive type to the output stream. output stream.

The method writeBytes is a useful The method writeBytes is a useful one. one.

How do I Create an Output How do I Create an Output Stream?Stream?-Server--Server-

On the server side, you can use the class On the server side, you can use the class PrintStream to send information to the client. PrintStream to send information to the client.

PrintStream output; PrintStream output; try { try { output = new output = new

PrintStream(serviceSocket.getOutputStream());PrintStream(serviceSocket.getOutputStream()); } } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); } }

How do I Create an Output How do I Create an Output Stream?Stream?-Server--Server-

You can use the class DataOutputStream as You can use the class DataOutputStream as mentioned mentioned

DataOutputStream output; DataOutputStream output; try { try { output = new output = new

DataOutputStream(serviceSocket.getOutputDataOutputStream(serviceSocket.getOutputStream()); Stream());

} } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); } }

How Do I Close Sockets?How Do I Close Sockets?-Client--Client-

You should always close the output and You should always close the output and input stream before you close the socket.input stream before you close the socket.

try { try { output.close(); output.close(); input.close(); input.close(); MyClient.close(); MyClient.close(); } } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); } }

How Do I Close Sockets?How Do I Close Sockets?-Server--Server-

try { try { output.close(); output.close(); input.close(); input.close(); serviceSocket.close();serviceSocket.close(); MyService.close(); MyService.close(); } } catch (IOException e) {catch (IOException e) { System.out.println(e); System.out.println(e); } }

ExamplesExamples-Client--Client-

When programming a client, you must follow these four When programming a client, you must follow these four steps:steps:

1.Open a socket. 1.Open a socket. 2.Open an input and output stream to 2.Open an input and output stream to

the Socket. the Socket. 3.Read from and write to the socket3.Read from and write to the socket

according to the server's protocol. according to the server's protocol. 4.Clean up.4.Clean up.

These steps are pretty much the same for all clients. The These steps are pretty much the same for all clients. The

only step that varies is step three, since it depends on the only step that varies is step three, since it depends on the server you are talking to. server you are talking to.

Echo Client ExampleEcho Client Example

ExamplesExamples-Server--Server-

In following example, Basically, the In following example, Basically, the echo server receives text from the echo server receives text from the client and then sends that exact text client and then sends that exact text back to the client. back to the client.

Echo Server ExampleEcho Server Example

Thank you….Thank you….

Discussion & CommentsDiscussion & Comments