Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7190911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:37:04+00:00 2026-05-28T19:37:04+00:00

Ok so I have constructed a working example of a client and server which

  • 0

Ok so I have constructed a working example of a client and server which can accept multiple client connections. My problem is is that I cannot connect a client which is not running the same internet connection as the one the server is being hosted on. Is this possible using server sockets?

Here is the code for my server:

import java.io.IOException;
import java.net.*;

public class MultipleSocketServer {

public static Socket connection;
public static String name = "Tyler's Server";
public static int limit = 2;
public static Thread[] clients = new Thread[limit];
public static int current = 0;
public static int port = 25565;
public static String[] connected = new String[limit];
public static ServerSocket socket;

public static void main(String[] args) {
    System.out.println("Server starting...");
    for(int i = 0; i < limit; i++) {
        connected[i] = "";
    }
    try {
        ServerSocket socket = new ServerSocket(port);
        while(true) {
            Socket connection = socket.accept();
            String ip = connection.getRemoteSocketAddress().toString().substring(1, 13);
            loop:
            for(int i = 0; i < connected.length; i++) {
                if(connected[0].equals(ip) || connected[1].equals(ip)) {
                    break loop;
                }else if(!connected[i].equals(ip)) {
                    connected[i] = ip;
                    MultiServer_Client client = new     MultiServer_Client(connection, i);
                    Thread run = new Thread(client);
                    run.start();
                    break loop;
                }
            }
        }
    } catch (IOException e1) {
        System.out.println("Could not bind server on: " + port);
        System.exit(-1);
    }
}
}

And here is the rest:

import java.io.*;
import java.net.Socket;

public class MultiServer_Client implements Runnable {

public String time;
public Socket client;
public StringBuffer process = new StringBuffer();

public BufferedInputStream inputStream;
public InputStreamReader reader;

public BufferedOutputStream outputStream;
public OutputStreamWriter writer;

public StringVis check = new StringVis("");
public StringChangeListener checkListener = new StringChangeListener() {
    public void textChanged(StringChangeEvent e) {
        System.out.println("Changed");
        write("Server recieved message...");
    }
};

public boolean connected = true;
public int ID;

public MultiServer_Client(Socket connection, int i) {
    client = connection;
    ID = i;
    try {
        //declare text input/output
        inputStream = new BufferedInputStream(client.getInputStream());
        reader = new InputStreamReader(inputStream);
        outputStream = new BufferedOutputStream(client.getOutputStream());
        writer = new OutputStreamWriter(outputStream, "US-ASCII");
    } catch (IOException e) {
        System.out.println("IOException: " + e);
    }
    System.out.println(MultipleSocketServer.connected[ID] + " connected...");
    write("Connected to " + MultipleSocketServer.name);
}

public void run() {
    while(connected) {
        read();
    }
    System.out.println("Disconnecting client...");
}

public void write(String authen) {
    try {
        time = new java.util.Date().toString();
        String message = time + ": " + authen + (char) 13;
        writer.write(message);
        writer.flush();
    } catch (IOException e) {
        connected = false;
        MultipleSocketServer.connected[ID] = "";
    }
}

public void read() {
    //read from client
    int character;
    process = new StringBuffer();
    try {
        while ((character = reader.read()) != 13) {
            process.append((char) character);
        }
        check.setText(process.toString());
        process.delete(0, process.length());
    } catch (IOException e) {
        connected = false;
        MultipleSocketServer.connected[ID] = "";
    }
}
}

Here’s the client code:

import java.net.*;
import java.util.Scanner;
import java.io.*;

public class SocketClient {

public static String host = "69.182.134.79";
public static int port = 25565;

public static void main(String [] args) {
    StringBuffer imports = new StringBuffer();
    String time;
    System.out.println("Client starting...");

    try {
        //establish client
        InetAddress address = InetAddress.getByName(host);
        Socket connection = new Socket(address, port);

        BufferedOutputStream bos = new     BufferedOutputStream(connection.getOutputStream());
        OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");

        BufferedInputStream bis = new     BufferedInputStream(connection.getInputStream());
        InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");

        while(true) {
            Scanner scan = new Scanner(System.in);
            String message = scan.nextLine();

            //write to server
            time = new java.util.Date().toString();
            String process = host + ":" + port + " sent data at " +     time + ": " + message + (char) 13;
            osw.write(process);
            osw.flush();

            //read from server
            int c;
            while ((c = isr.read()) != 13) {
                imports.append((char) c);
            }
            System.out.println(imports);
            imports.replace(0, imports.length(), "");

            if(message.equals("--EXIT")) {
                connection.close();
            }
        }

    } catch (UnknownHostException e) {
        System.out.println("UnknownHostException: " + e);
    } catch (IOException e) {
        System.out.println("IOExcepion: " + e);
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:37:05+00:00Added an answer on May 28, 2026 at 7:37 pm

    Change

    MultiServer_Client client = new MultiServer_Client(connection, i);

    to

    MultiServer_Client client = new MultiServer_Client(new Socket([Server IP], port), i);

    This should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I have a particular object which can be constructed from a file, as
I'm working on someone's code and they have a constructor that uses: class qwerty
Python Decimal doesn't support being constructed from float; it expects that you have to
Suppose, I have a lot of classes, which are constructed using Java reflection (for
I need to query some data. here is the query that i have constructed
I am working on an ASPX page that needs to handle multiple different kinds
I'm working on an application that is divided in a thin client and a
I have been working with the example code from the ExecutorCompletionService and put together
Does anyone have a working example of using the new GWT constructs for RequestFactory
I am new to OOP and I have been working on this example but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.