What Will I Learn?
- You will learn how to create client and server program
- You will learn how to make communication between two program
- You will learn how to create connection between client and server
- You will learn to create two class in one package on netbeans
Requirements
- you must understand basic of java programming
- you must understand how to create stucture beetween two progran on the package
- you must know how to implement java program on netbeans IDE
- The Java Software Development Kit must be installed on your system
- Netbeans IDE should also be installed on your system
Difficulty
- Basic
Tutorial Contents
- first open netbeans IDE aplication
i use netbeans IDE 7.0.1 version - second, create new project, select java category, and in project select java aplication, then press next.
- then write down the name of your project, hit finish
- the next step is to create a file with the name client.java as in the picture below.
- type the program below into the client program field, and I will explain a bit about the program below.
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();
}}
- below is an explanation of the above program.
package clientserver;
this is the name of the package where the client server program is located
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();
- After creating the client program we will create a server program, create a file with the name Server.java.
- then type the program below in Server.java file
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(); }}
- the difference between the client program and the server program is on the syntax
Socket Sock=serverSock .accept();
which serves to receive requests from the client. - after we create both programs then we try to run both programs as shown below.
- run both.
- then we will see the output.
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.
Curriculum
Place here a list of related tutorials you have already shared on Utopian that make up a Course Curriculum, if applicable.
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.
You can contact us on Discord.
[utopian-moderator]
Hey @sdtyldz, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!