Repository
https://github.com/dmlloyd/openjdk
What Will I Learn?
- You will learn how to create connection with socket programming
- You will learn connect two program in one package
- You will learn how to create method like chatting Apps
Requirements
- you must know basic of java programming
- you must know about socket programming
- you must know how to set port on two program
Difficulty
- Intermediate
Tutorial Contents
okay, on this occasion I will create a tutorial on how to create a connection using socket programming, socket programming is very widely used in large applications such as chat application or application sender file . and this time I will make the method.
- the first step you should do is create a new package in NetBeans.
then name it for the package you have created
- then create a new class by name server.java
- then type the program below, I will explain it.
package socketprogramming;
import java.io.*;
import java.net.*;
public class server{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
server(){}
void run()
{
try{
providerSocket = new ServerSocket(2004, 10);
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection with IP " + connection.getInetAddress().getHostName());
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Program is successful Connected");
do{
try{
message = (String)in.readObject();
System.out.println("client>" + message);
if (message.equals("hi....!"))
sendMessage("Hi... to");
}
catch(ClassNotFoundException classnot){
System.err.println("Data received ");
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
}
finally{
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException){
}
}
public static void main(String args[])
{
server server = new server();
while(true){
server.run();
}
}
}
- okay, I'll explain the important parts of the above program:
package socketprogramming;
this is the declaration of the package name, make sure you create both programs in the same package.import java.io. *;
this is a class that works for input and output process.- below is the process of creating object socket programming that we will call in the program.
ServerSocket providerSocket;
Socket connection = null;
- this is the declaration of the input object, output, and to display the message.
ObjectOutputStream out;
ObjectInputStream in;
String message;
server ()
This is a class declaration server.- this is the process of calling the object we have created before.
providerSocket = new ServerSocket(2004, 10);
System.out.println("Waiting for connection");
connection = providerSocket.accept();
this is the process of receiving connections.- This works to show that the system has been successfully connected with the IP address that has been determined.
System.out.println("Connection with IP " + connection.getInetAddress().getHostName());
- below serves to display the input, output and connection
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Program is successful Connected");
- this is the conditioning of the situation that occurs when the program is run.
do{
try{
message = (String)in.readObject();
System.out.println("client>" + message);
if (message.equals("hi....!"))
sendMessage("Hi... to"); }
catch(ClassNotFoundException classnot){
System.err.println("Data received ");
}}while(!message.equals("bye"));
}catch(IOException ioException){
}
- this is to close the programming socket.
in.close();
out.close();
providerSocket.close();
- okay, I think it's enough explanation for the server program, then we will create a class client
- type the program below for the class client
package socketprogramming;
import java.io.*;
import java.net.*;
public class client{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
client(){}
void run()
{
try{requestSocket = new Socket("localhost", 1718);
System.out.println("Connected to localhost in port 1718");
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
do{
try{
message = (String)in.readObject();
System.out.println("server>" + message);
sendMessage("Hi my server");
message = "bye";
sendMessage(message);
}
catch(ClassNotFoundException classNot){
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){ }
finally{
try{in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
}}}
void sendMessage(String msg)
{try{
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);}
catch(IOException ioException){
}}public static void main(String args[])
{ client client = new client();
client.run();
}}
- I do not think much need to explain again to the second program because basically almost the same, but only different in some part only.
Socket requestSocket;
this is for connection requesttry {requestSocket = new Socket (" localhost ", 1718);
and this is for connection creationSystem.out.println (" Connected to localhost in port 1718 ");
This is for notification that the program is successfully connected.- okay, both programs we have created, then we will try to run the program if it will successfully connected.
- we just to right click on the server program and select run file.
- if the program successfully connected then it will look like this.
- Okay, the program is done, thank you for following my tutorial to complete.
Curriculum
- Java Tutorial : How To Create java JFrame/GUI To Play Music On NetBeans
- Java Tutorial: How To Select And Send All File In One Folder Using "for" Looping Method On NetBeans
- HOW TO CREATE ACTION TO AUTO SELECT FILE USING "if" CONDITIONAL ON JAVA
- Java Tutorial: How To Create GUI For Send And Receive File Application On NetBeans
- Java Tutorial: How To Create a Program To Send And Receive Files On NetBeans
Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:
Looking forward to your upcoming tutorials.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Hey @lapulga
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
Nice informative thanks dear
You made this platform as a source of knowledge, it's really a great idea
I've got a new tutor,
Thanks a lot.
Dude, you should do it with Thread. One Thread for each incoming client. Build a class, that extends java.lang.Thread.
You will get a much cleaner code and the handlling of the clients is much easier.
Also, endless loops are never good. Make an escape point to the loop, e. g. buy pressing a key on the server console.