package clientserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class Client {
public static void main(String[] args)throws IOException {
Socket sock=new Socket("localhost", 6066);
DataInputStream in= new DataInputStream(sock.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out =new DataOutputStream(sock.getOutputStream());
out.writeUTF("what is youre name");
sock.close();
}}
package clientserver;import java.io.DataInputStream;
Java DataInputStream class lets you read Java primitives (int, float, long etc.)
import java.io.DataOutputStream;
import java.io.DataOutputStream; allows the application to write Java primitive data types to output in a portable way. The application can then use the data input stream to re-read the data.
import java.io.IOException;
This class is a general class of exceptions generated by failed or interrupted I / O operations.
import java.net.Socket;
This class works to listen to connections that have occurred and ready to receive requests from other processes.
public class Client {
declaration for the class client
public static void main(String[] args)throws IOException {
public static void main is an element that must exist in java programming language
Socket sock=new Socket("localhost", 6066);
This is the declaration of a new object named "sock"
DataInputStream in= new DataInputStream(sock.getInputStream());
This is the shortcut of objects in the DatasInputStream class
System.out.println(in.readUTF());
Command to display the output to the monitor
DataOutputStream out =new DataOutputStream(sock.getOutputStream());
This is the creation of the output object in the DataInputStream class
out.writeUTF("what is youre name");
this is the command to display the text on the output
sock.close();
package clientserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;3
public class Server {
public static void main(String[] args) throws IOException{
ServerSocket serverSock=new ServerSocket(6066);
Socket Sock=serverSock .accept();
DataOutputStream out =new DataOutputStream(Sock.getOutputStream());
out.writeUTF("my name is lapulga");
DataInputStream in= new DataInputStream(Sock.getInputStream());
System.out.println(in.readUTF());
Sock.close(); }}
Socket Sock=serverSock .accept(); which serves to receive requests from the client.client output:
server output:
application using this method:
work principle
this method serves to connect between the client with the server on the program above shows that we try to send a message to the server and then the server answered. this method is very much used in chatingan applications such as yahoo mesengger and others.
Place here a list of related tutorials you have already shared on Utopian that make up a Course Curriculum, if applicable.