Register   Login   About   Study   Enterprise   Share
Internet / AI Technology University (ITU/AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 3. Threads and Network >> 3.2. Network
Current Topic: 3.2.2. Client Socket Example
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
This source below is an example of the client socket request to a server. Client sends a request for service and receives back a response.

package its.day15.sockets;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ClientSocket {
/**
* clientSocketRequest() method establishes a socket connection and sends a request for service
* The operation is inside the try - catch statements as any network operation.
* The method creates a socket object with the specified host (server name or IP address) and the specified port number.
* Then the method establishes Input and Output streams connected to the socket.
* Via the DataOutputStream the socket sends request for service to a server.
* The socket receives a response from a server via the DataInputStream
* Then the method closes the streams and closes the socket connection.

* @param host
* @param portNumber
* @param serviceRequest
* @return response or error message if failure
* @@author Jeff.Zhuk@Javaschool.com
*/
public String clientSocketRequest(String host, int portNumber, String serviceRequest) {
try {
// request a socket connection providing server host and portNumber
Socket socket = new Socket(host, portNumber);
// create i/o streams from the socket
OutputStream outStrm = socket.getOutputStream();
InputStream inStrm = socket.getInputStream();
DataInputStream dis = new DataInputStream(inStrm);
DataOutputStream dos = new DataOutputStream(outStrm);

// send serviceRequest to the server via the DataOutputStream
dos.writeUTF(serviceRequest);

// receiving response from the server via the DataInputStream
String response = new String(dis.readUTF());

// close streams and socket
dos.close();
dis.close();
socket.close();
return response;
} catch (Exception e) {
return ("ERROR: TCP/IP Connection failure: " + e);
}
}
/**
* Test the client socket
* The server socket must be running on localhost on the port 5555
* @param args
*/
public static void main(String[] args) {
ClientSocket client = new ClientSocket();
String response = client.clientSocketRequest("localhost", 5555, "Hello");
System.out.println(response);
}
}
Was it clear so far?


Assignments:

1. Open Eclipse and create the project week7.network
2. Under the project ? src ? create the package its.day15.sockets
3. Right click on the package ? create a NEW ? CLASS - ClientSocket.
4. Type the source of this class provided above.
5. Get rid of red lines and Run ? As Java Application
6. This program will run without any message in the console waiting for server response
7. But it will not come! Why? The server program is not in place yet!
8. Stop the program and move to the next session to get the server side ready?
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>package its.day15.sockets;
<br/>
<br/>import java.io.DataInputStream;
<br/>import java.io.DataOutputStream;
<br/>import java.io.InputStream;
<br/>import java.io.OutputStream;
<br/>import java.net.Socket;
<br/>
<br/>public class ClientSocket {
<br/>	/**
<br/>	 * clientSocketRequest() method establishes a socket connection and sends a request for service 
<br/>	 * The operation is inside the try - catch statements as any network operation.
<br/>	 * The method creates a socket object with the specified host (server name or IP address) and the specified port number.
<br/>	 * Then the method establishes Input and Output streams connected to the socket.
<br/>	 * Via the DataOutputStream the socket sends request for service to a server.
<br/>	 * The socket receives a response from a server via the DataInputStream
<br/>     * Then the method closes the streams and closes the socket connection.
<br/>
<br/>	 * @param host
<br/>	 * @param portNumber
<br/>	 * @param serviceRequest
<br/>	 * @return response or error message if failure
<br/>	 * @@author Jeff.Zhuk@Javaschool.com
<br/>	 */
<br/>	public String clientSocketRequest(String host, int portNumber, String serviceRequest) {
<br/>		try {
<br/>			// request a socket connection providing server host and portNumber
<br/>			Socket socket = new Socket(host, portNumber);
<br/>			// create i/o streams from the socket
<br/>			OutputStream outStrm = socket.getOutputStream();
<br/>			InputStream inStrm = socket.getInputStream();
<br/>			DataInputStream dis = new DataInputStream(inStrm);
<br/>			DataOutputStream dos = new DataOutputStream(outStrm);
<br/>
<br/>			// send serviceRequest to the server via the DataOutputStream
<br/>			dos.writeUTF(serviceRequest);
<br/>
<br/>			// receiving response from the server via the DataInputStream
<br/>			String response = new String(dis.readUTF());
<br/>
<br/>			// close streams and socket
<br/>			dos.close();
<br/>			dis.close();
<br/>			socket.close();
<br/>			return response;
<br/>		} catch (Exception e) {
<br/>			return ("ERROR: TCP/IP Connection failure: " + e);
<br/>		}
<br/>	}
<br/>	/**
<br/>	 * Test the client socket
<br/>	 * The server socket must be running on localhost on the port 5555
<br/>	 * @param args
<br/>	 */
<br/>	public static void main(String[] args) {
<br/>		ClientSocket client = new ClientSocket();
<br/>		String response = client.clientSocketRequest("localhost", 5555, "Hello");
<br/>		System.out.println(response);
<br/>	}
<br/>}
<br/>






Was it clear so far?



Assignments:

1. Open Eclipse and create the project week7.network
2. Under the project ? src ? create the package its.day15.sockets
3. Right click on the package ? create a NEW ? CLASS - ClientSocket.
4. Type the source of this class provided above.
5. Get rid of red lines and Run ? As Java Application
6. This program will run without any message in the console waiting for server response
7. But it will not come! Why? The server program is not in place yet!
8. Stop the program and move to the next session to get the server side ready?

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy