https://github.com/dmlloyd/openjdk
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.
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();
}
}
}
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.ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
server () This is a class declaration server.providerSocket = new ServerSocket(2004, 10);
System.out.println("Waiting for connection");
connection = providerSocket.accept();this is the process of receiving connections.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){
}
in.close();
out.close();
providerSocket.close();
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();
}}
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.https://gist.github.com/andilapulga/35c1d91d6964f05f58654076f93ccd40