Techiio-author
Started by Sohini PaulOct 31, 2021

Open
How to connect java socket to remote server socket

2 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWES SHARE

I am able to set up connection between two gadgets which are on local network. But i need to attach computers that aren't on a neighborhood community. (P2P)

How to creat ServerSocket listen to public socket connections?

A way to understand the general public ip address of ServerSocket that we will use with far off socket to establish connection? Or how can I get list of ip addresses that I can strive?

1 Replies

Techiio-commentatorPOULAMI SARKAR replied 3 months ago0 likes0 dislikes

I think it's helpful for you,

you can connect the java socket to remote server socket like this:

import com.chilkatsoft.*;

public class ChilkatExample {

static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}

public static void main(String argv[])
{
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

CkSocket socket = new CkSocket();

// Connect to port 5555 of localhost.
// The string "localhost" is for testing on a single computer.
// It would typically be replaced with an IP hostname, such
// as "www.chilkatsoft.com".
boolean ssl = false;
int maxWaitMillisec = 20000;
boolean success = socket.Connect("localhost",5555,ssl,maxWaitMillisec);
if (success != true) {
System.out.println(socket.lastErrorText());
return;
}

// Set maximum timeouts for reading an writing (in millisec)
socket.put_MaxReadIdleMs(10000);
socket.put_MaxSendIdleMs(10000);

// Pretend, for the sake of the example, that the
// ficticious server is going to send a "Hello World!"
// after accepting the connection.
// Note: Technically, the ReceiveString may not receive the
// complete string, although it's highly probable given the short
// length of the "Hello World!" message.
// See this Chilkat blog post for more information:
// http://www.cknotes.com/?p=302
String receivedMsg = socket.receiveString();
if (socket.get_LastMethodSuccess() != true) {
System.out.println(socket.lastErrorText());
return;
}

// Close the connection with the server
// Wait a max of 20 seconds (20000 millsec)
success = socket.Close(20000);

System.out.println(receivedMsg);
}
}
You must be Logged in to reply
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance