This is a simple client-server program. It works fine when executed in a single computer. But, it doesn't work when executed in two different laptops connected using WiFi.
Here's the code:
Server:
public class Server
{
private static int port;
private ServerSocket serverSocket;
public void GreetingServer(int port) throws IOException
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(20000);
while(true)
{
try
{
System.out.println("Waiting for client");
Socket server = serverSocket.accept();
System.out.println("Connected");
DataInputStream in =new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF("Thank you for connecting to ");
server.close();
}
catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}
catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
public static void main(String [] args)
{
port=9001;
try
{
Server s=new Server();
s.GreetingServer(port);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Client:
public class Client
{
private static String serverName;
public static void main(String [] args)
{
String sName = "192.168.xxxx.x";
int port = 9001;
try
{
System.out.println("Connecting to " + sName
+ " on port " + port);
Socket client = new Socket(sName, port);
System.out.println("Connected");
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello from " + client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
}catch(IOException e)
{
}
}
}
What could be the problem? Please me..
Thanks in advance..
Related
I'm planning to create a server and run it on google cloud VM, this code is working fine in the localhost but it won't run on External IP.
import java.net.*;
import java.io.*;
public class Server extends Thread {
private ServerSocket serverSocket;
public Server(int port) throws IOException {
serverSocket = new ServerSocket(port);
}
public void run() {
while(true) {
try {
System.out.println("Waiting for Connection ");
Socket server = serverSocket.accept();
System.out.println("Just connected to " + server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF("Goodbye");
server.close();
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
public static void main(String [] args) throws UnknownHostException {
int port = 4999;
try {
Thread t = new Server(port);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and this is for the client
import java.net.*;
import java.io.*;
public class Client {
public static void main(String [] args) {
String serverName = "localhost";
int port = Integer.parseInt("4999");
try {
System.out.println("Connecting to " + serverName + " on port " + port);
Socket client = new Socket(serverName, port);
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("[Client]Hello from " + client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("[Server] " + in.readUTF());
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and this is the net config of the google VM instance:
Internal ip-> 10.138.0.13 (nic0) | External ip-> 34.83.94.126
can you please tell me how to get it running on a public ip.
I am trying to establish a basic connection with my server from the client program. When I run my client/server on the same network, it works fine, but I don't know where to start when my client program is on my PC but my server program is on the EC2 instance.
I have set up an EC2 linux instance on AWS, and have loaded the server code onto it. The Client is on my PC.
To run the server, I'm using:
java GreetingServer 6000
To run the client, I'm using:
java GreetingClient <EC2 Public IP> 6000
I'm new to AWS and server-client communication, so if you know any good tutorials for this, that would be great!
Server Code:
// File Name GreetingServer.java
import java.net.*;
import java.io.*;
public class GreetingServer extends Thread {
private ServerSocket serverSocket;
public GreetingServer(int port) throws IOException {
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
}
public void run() {
while(true) {
try {
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to " + server.getRemoteSocketAddress());
DataInputStream in = new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress()
+ "\nGoodbye!");
server.close();
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
public static void main(String [] args) {
int port = Integer.parseInt(args[0]);
try {
Thread t = new GreetingServer(port);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client Code:
// File Name GreetingClient.java
import java.net.*;
import java.io.*;
public class GreetingClient {
public static void main(String [] args) {
String serverName = args[0];
int port = Integer.parseInt(args[1]);
try {
System.out.println("Connecting to " + serverName + " on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to " + client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello from " + client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have tomcat installed on vps, everything has been configured, but how do I host my java server on it? What extension should I use and which folder should I put? Or do I need to change something in the Server.java?
Here he is:
public class Server {
public static void main(String[] ar) throws IOException {
int port = 8080;
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("Waiting for a client...");
Socket socket = ss.accept();
System.out.println("Got a client!");
System.out.println();
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String line = null;
while(true) {
line = in.readUTF();
System.out.println("The dumb client just sent me this line : " + line);
System.out.println("I'm sending it back...");
out.writeUTF(line);
out.flush();
System.out.println("Waiting for the next line...");
System.out.println();
}
} catch(Exception x) { x.printStackTrace(); }
}
}
I am trying to make a chat server and client that can communicate over two separate computers connected to the internet. One of them is connected to wifi and another one is through a modem. Here is my server code.
GreetingServer.java
import java.net.*;
import java.io.*;
public class GreetingServer extends Thread
{
private ServerSocket serverSocket;
public GreetingServer(int port) throws IOException
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(100000);
}
public void run()
{
while(true)
{
try
{
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to "
+ server.getRemoteSocketAddress());
DataInputStream in =
new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out =
new DataOutputStream(server.getOutputStream());
out.writeUTF("Thank you for connecting to "
+ server.getLocalSocketAddress() + "\nGoodbye!");
server.close();
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
public static void main(String [] args)
{
int port = Integer.parseInt("6066");
try
{
Thread t = new GreetingServer(port);
t.start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
GreetingClient.java
import java.net.*;
import java.io.*;
public class GreetingClient2
{
public static void main(String [] args)
{
String serverName = "10.2.3.100";
int port = Integer.parseInt("6066");
try
{
System.out.println("Connecting to " + serverName +
" on port " + port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello from "
+ client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in =
new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
The IP address in Client code is of the computer where the server is running acquired by InetAddress.getLocalHost().getHostAddress(). When I run the server, it waits for the client. And when I run the client code from another computer, it doesn't connect. Is it possible to communicate like this through sockets in java ?
Yes, it is possible.
Your server has to have an external ip.
Also if your server is connected via WiFi, check router firewall settings.
I have written a program where the client should be able to establish connection, and write to som message to the server. And the server should print this message.
The problem is that once the client connect to the server, I get an exception with the following message:
java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at Client.<init>(Client.java:21)
at Server.startServer(Server.java:42)
at Server.main(Server.java:62)
My code for the server is as shown below:
import java.io.*;
import java.net.*;
public class Server {
private int port = 5555;
public void printServerAddress() {
InetAddress i = null;
String host = null;
try {
i = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
host = i.getHostAddress();
System.out.println("Contact this server at address: " + host + " and port: " + port);
}
public void startServer() {
ServerSocket serverSocket = null;
BufferedReader reader = null;
Socket socket = null;
String fromClient = null;
try {
serverSocket = new ServerSocket(port);
while(true) {
socket = serverSocket.accept();
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
if(socket != null) {
System.out.println("Connection from: " + socket);
}
Thread thread = new Thread(new Client(socket));
thread.start();
while((fromClient = reader.readLine()) != null) {
System.out.println("From client: " + fromClient);
}
reader.close();
socket.close();
}
} catch (IOException e) {
}
}
public static void main(String[] args) {
Server server = new Server();
server.printServerAddress();
server.startServer();
}
}
And the code for the client is:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client implements Runnable{
private int port;
private String serverAddress;
private PrintWriter writer;
private BufferedReader reader;
public Client(Socket socket)
{
try {
socket = new Socket(serverAddress, port);
writer = new PrintWriter(socket.getOutputStream(), true);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
System.out.println("Write message to server:");
Scanner scanner = new Scanner(System.in);
while(scanner.next() != "quit") {
writeToServer(scanner.next());
}
writer.close();
}
public void writeToServer(String message) {
writer.write(message);
}
public void readFromServer() {
String msg = null;
try {
while((msg = reader.readLine()) != null) {
System.out.println("From server: " + msg);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("localhost", 5555);
new Client(s);
}
}
Any idea on what I did wrong, and how to fix my problem?
socket = new Socket(serverAddress, port);
serverAddress is null and port is 0.
try to define the serverAddress variable and the port before call new Socket(serverAddress, port)
or change
socket = new Socket(serverAddress, port);
to
this.socket = socket;
edit
you can change your constructer to :
public Client(String serverAddress, int port)
{
try {
Socket socket = new Socket(serverAddress, port);
writer = new PrintWriter(socket.getOutputStream(), true);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}