Send data client server java

Java socket programming Tutorial – How to code Client and Server

This is a quick guide/tutorial to learning socket programming in Java.

To summarise the basics, sockets are the fundamental «things» behind any kind of network communications done by your computer.

For example when you type www.google.com in your web browser, it opens a socket and connects to google.com to fetch the page and show it to you. Same with any chat client like gtalk or skype.

In general any network communication goes through a socket.

Before you begin

This tutorial assumes that you already have a basic knowledge of java and can compile and run java programs.

So lets begin with sockets.

1. Creating a socket

This first thing to do is create a socket.

The above creates a socket object that can be used for other operations like connecting and sending data.

The socket class is actually a TCP socket, although its not specified (like its done in C or python for example).

Also, it is a «client socket» since it can be used only to make socket clients and not servers. More on this later in this article.

UDP sockets in java are created using the class DatagramSocket, and we shall look into this in another article.

Ok , so you have created a socket successfully. But what next ? Next we shall try to connect to some server using this socket. We can connect to www.google.com

2. Connect to a Server

We connect to a remote server on a certain port number. So we need 2 things , IP address and port number to connect to. So you need to know the IP address of the remote server you are connecting to. Here we used the ip address of google.com as a sample.

Now that we have the ip address of the remote host/system, we can connect to ip on a certain ‘port’ using the connect function.

//java socket client example import java.io.*; import java.net.*; public class socket_client < public static void main(String[] args) throws IOException < Socket s = new Socket(); String host = "www.google.com"; try < s.connect(new InetSocketAddress(host , 80)); >//Host not found catch (UnknownHostException e) < System.err.println("Don't know about host : " + host); System.exit(1); >System.out.println("Connected"); > >
$ javac socket_client.java && java socket_client Connected

It creates a socket and then connects. Try connecting to a port different from port 80 and you should not be able to connect which indicates that the port is not open for connection. This logic can be used to build a port scanner.

The Socket object can be connected at the time of creation.

Socket s = new Socket(host , port);

OK, so we are now connected. Lets do the next thing , sending some data to the remote server.

3. Sending Data

//java socket client example import java.io.*; import java.net.*; public class socket_client < public static void main(String[] args) throws IOException < Socket s = new Socket(); String host = "www.google.com"; PrintWriter s_out = null; try < s.connect(new InetSocketAddress(host , 80)); System.out.println("Connected"); //writer for socket s_out = new PrintWriter( s.getOutputStream(), true); >//Host not found catch (UnknownHostException e) < System.err.println("Don't know about host : " + host); System.exit(1); >//Send message to server String message = "GET / HTTP/1.1\r\n\r\n"; s_out.println( message ); System.out.println("Message send"); > >

In the above example , we first connect to www.google.com and then send the string message «GET / HTTP/1.1\r\n\r\n» to it. The message is actually an «http command» to fetch the mainpage of a website.

Now that we have send some data , its time to receive a reply from the server. So lets do it.

4. Receiving Data

//java socket client example import java.io.*; import java.net.*; public class socket_client < public static void main(String[] args) throws IOException < Socket s = new Socket(); String host = "www.google.com"; PrintWriter s_out = null; BufferedReader s_in = null; try < s.connect(new InetSocketAddress(host , 80)); System.out.println("Connected"); //writer for socket s_out = new PrintWriter( s.getOutputStream(), true); //reader for socket s_in = new BufferedReader(new InputStreamReader(s.getInputStream())); >//Host not found catch (UnknownHostException e) < System.err.println("Don't know about host : " + host); System.exit(1); >//Send message to server String message = "GET / HTTP/1.1\r\n\r\n"; s_out.println( message ); System.out.println("Message send"); //Get response from server String response; while ((response = s_in.readLine()) != null) < System.out.println( response ); >> >

Here is the output of the above code :

google.com replied with the content of the page we requested. Quite simple!
Now that we have received our reply, its time to close the socket.

5. Close socket

Function close is used to close the socket.

//close the i/o streams s_out.close(); in.close(); //close the socket s.close();

Lets Revise

So in the above example we learned how to :
1. Create a socket
2. Connect to remote server
3. Send some data
4. Receive a reply

Its useful to know that your web browser also does the same thing when you open www.google.com
This kind of socket activity represents a CLIENT. A client is a system that connects to a remote system to fetch data.

The other kind of socket activity is called a SERVER. A server is a system that uses sockets to receive incoming connections and provide them with data. It is just the opposite of Client. So www.google.com is a server and your web browser is a client. Or more technically www.google.com is a HTTP Server and your web browser is an HTTP client.

Now its time to do some server tasks using sockets.

Server Programming

OK now onto server things. Servers basically do the following :

1. Open a socket
2. Bind to a address(and port).
3. Listen for incoming connections.
4. Accept connections
5. Read/Send

We have already learnt how to open a socket. So the next thing would be to bind it.

1. Bind socket to a port

To create a socket and bind it to a particular port number, all that needs to be done is to create an object of class ServerSocket. The constructor takes 2 parameters, first is the local port number and the 2nd is the backlog number.

ServerSocket s = new ServerSocket(5000 , 10);

The above piece of code will create a socket and bind it to local machine port number 5000. Its important to note that the socket is already listening for connections. So the next thing is to accept connections by calling the accept method on the socket.

2. Accept connection

Function accept is used for this.

//java server example import java.io.*; import java.net.*; public class socket_server < public static void main(String args[]) < ServerSocket s = null; Socket conn = null; PrintStream out = null; BufferedReader in = null; String message = null; try < //1. creating a server socket - 1st parameter is port number and 2nd is the backlog s = new ServerSocket(5000 , 10); //2. Wait for an incoming connection echo("Server socket created.Waiting for connection. "); //get the connection socket conn = s.accept(); //print the hostname and port number of the connection echo("Connection received from " + conn.getInetAddress().getHostName() + " : " + conn.getPort()); //3. get Input and Output streams out = new PrintStream(conn.getOutputStream()); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); out.println("Welcome. Server version 1.0"); out.flush(); >catch(IOException e) < System.err.println("IOException"); >//5. close the connections and stream try < in.close(); out.close(); s.close(); >catch(IOException ioException) < System.err.println("Unable to close. IOexception"); >> public static void echo(String msg) < System.out.println(msg); >>

Run the program. It should show

$ javac socket_server.java && java socket_server Server socket created.Waiting for connection.

So now this program is waiting for incoming connections on port 5000. Dont close this program , keep it running.
Now a client can connect to it on this port. We shall use the telnet client for testing this. Open a terminal and type

$ telnet localhost 5000 Trying 127.0.0.1. Connected to localhost. Escape character is '^]'. Welcome. Server version 1.0 Connection closed by foreign host.

And the server output will show

$ javac socket_server.java && java socket_server Server socket created.Waiting for connection. Connection received from localhost : 34513

So we can see that the client connected to the server. Try the above steps till you get it working perfect.

We accepted an incoming connection but closed it immediately. This was not very productive. There are lots of things that can be done after an incoming connection is established. Afterall the connection was established for the purpose of communication.

3. Simple ECHO server

Now lets modify the above program such that it takes some input from client and replies back with the same message.

//java server example import java.io.*; import java.net.*; public class socket_server < public static void main(String args[]) < ServerSocket s = null; Socket conn = null; PrintStream out = null; BufferedReader in = null; String message = null; try < //1. creating a server socket - 1st parameter is port number and 2nd is the backlog s = new ServerSocket(5000 , 10); //2. Wait for an incoming connection echo("Server socket created.Waiting for connection. "); //get the connection socket conn = s.accept(); //print the hostname and port number of the connection echo("Connection received from " + conn.getInetAddress().getHostName() + " : " + conn.getPort()); //3. get Input and Output streams out = new PrintStream(conn.getOutputStream()); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); out.println("Welcome. Server version 1.0"); out.flush(); //4. The two parts communicate via the input and output streams do < //read input from client message = (String)in.readLine(); echo("client>" + message); if(message != null) < out.println(message); >else < echo("Client has disconnected"); break; >> while(!message.equals("bye")); > catch(IOException e) < System.err.println("IOException"); >//5. close the connections and stream try < in.close(); out.close(); s.close(); >catch(IOException ioException) < System.err.println("Unable to close. IOexception"); >> public static void echo(String msg) < System.out.println(msg); >>

Run the above code in 1 terminal. And connect to this server using telnet from another terminal and you should see this :

$ telnet localhost 5000 Trying 127.0.0.1. Connected to localhost. Escape character is '^]'. Welcome. Server version 1.0 hi hi how are you how are you

So the client(telnet) received a reply from server.

In the above example we can see that the server is handling the client very well. But there is a problem. It can only handle one client at a time. If other clients connect to it, they would get connected but nothing more would happen.

4. Handling Multiple Connections

To handle every connection we need a separate handling code to run alongside the main server that is accepting new connections. One way to achieve this is using threads. The main server program accepts a connection and creates a new thread to handle communication for the connection, and then the server goes back to accept more connections.

We shall now use threads to create handlers for each connection the server accepts.

//java server example import java.io.*; import java.net.*; public class socket_server < public static void main(String args[]) < ServerSocket s = null; Socket conn = null; try < //1. creating a server socket - 1st parameter is port number and 2nd is the backlog s = new ServerSocket(5000 , 10); //2. Wait for an incoming connection echo("Server socket created.Waiting for connection. "); while(true) < //get the connection socket conn = s.accept(); //print the hostname and port number of the connection echo("Connection received from " + conn.getInetAddress().getHostName() + " : " + conn.getPort()); //create new thread to handle client new client_handler(conn).start(); >> catch(IOException e) < System.err.println("IOException"); >//5. close the connections and stream try < s.close(); >catch(IOException ioException) < System.err.println("Unable to close. IOexception"); >> public static void echo(String msg) < System.out.println(msg); >> class client_handler extends Thread < private Socket conn; client_handler(Socket conn) < this.conn = conn; >public void run() < String line , input = ""; try < //get socket writing and reading streams DataInputStream in = new DataInputStream(conn.getInputStream()); PrintStream out = new PrintStream(conn.getOutputStream()); //Send welcome message to client out.println("Welcome to the Server"); //Now start reading input from client while((line = in.readLine()) != null && !line.equals(".")) < //reply with the same message, adding some text out.println("I got : " + line); >//client disconnected, so close socket conn.close(); > catch (IOException e) < System.out.println("IOException on socket : " + e); e.printStackTrace(); >> >

Run the above server and open 3 terminals like before. Now the server will create a thread for each client connecting to it.

The telnet terminals would show :

$ telnet localhost 5000 Trying 127.0.0.1. Connected to localhost. Escape character is '^]'. Welcome to the Server hi I got : hi how are you I got : how are you i am fine I got : i am fine

The server terminal might look like this

$ javac socket_server.java && java socket_server Server socket created.Waiting for connection. Connection received from localhost : 34907 Connection received from localhost : 35097

The above connection handler takes some input from the client and replies back with the same.

So now we have a server thats communicative. Thats useful now.

Conclusion

By now you must have learned the basics of socket programming in Java. You can try out some experiments like writing a chat client or something similar.

If you think that the tutorial needs some addons or improvements or any of the code snippets above dont work then feel free to make a comment below so that it gets fixed.

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

24 Comments

Источник

Читайте также:  Готовы сервер для css
Оцените статью