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 7623467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:42:49+00:00 2026-05-31T04:42:49+00:00

I’ve been working on this for a while now and I’ve hit a bit

  • 0

I’ve been working on this for a while now and I’ve hit a bit of a brick wall. I’ve tried googling lots but everything on the topic is either non server based or about using an API. I want to stick to the basic use of the java packages.

What I’m aiming for is two clients to be able to send messages back and forth, just in the console inside Eclipse would do for now. I know I’ll have to implement threads in the server to handle the two connections and probably have to write a protocol too for the server.

Can anyone give me any pointers on this? Help greatly appreciated

Here’s what I have so far

Client

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

public class Client 
{
static Socket client;
static String input;
static DataInputStream in;
static DataOutputStream out;
static BufferedReader keyboard;


//constructor, instantiation.
static void Open()
{
    try 
    {
        client = new Socket("127.0.0.1", 6666);
        InputStream sin = client.getInputStream();
        OutputStream sout = client.getOutputStream();

        in = new DataInputStream(sin);
        out = new DataOutputStream(sout);
    } 
    catch (UnknownHostException e) {
        System.err.println("Don't know about host: taranis.");
        System.exit(1);
    } 
    catch (IOException e) {
        System.err.println("Couldn't get I/O for " + "the connection to: Host.");
        System.exit(1);
    }

    keyboard = new BufferedReader(new InputStreamReader(System.in));

}

//close
static void Close() throws IOException
{
    client.close();
    keyboard.close();
    in.close();
    out.close();
}



public static void main(String[] args) throws IOException 
{

    Open();

    while(true) 
    {
        // wait for the user to type in something and press enter.
        input = keyboard.readLine(); 


        // send the above line to the server.
        out.writeUTF(input); 
        out.flush(); 

        // wait for the server to send a line of text.
        input = in.readUTF(); 

        System.out.println("The server was very polite. It sent me this : " + input);
        System.out.println();
    }


 }

 }

Host

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

public class Host 
{

//static variables
static ServerSocket host;
static Socket client;
static HostProtocol hp;
static String input1, output2;
static InputStream sin;
static OutputStream sout;
static DataInputStream in;
static DataOutputStream out;

//constructor
public Host() throws IOException
{
    hp = new HostProtocol();
    Open();

     sin = client.getInputStream();
     sout = client.getOutputStream();
     in = new DataInputStream(sin);
     out = new DataOutputStream(sout);
}

//open sockets, instantiate variables
public static void Open()
{
    try 
    {
        host = new ServerSocket(6666);
    } 
    catch (IOException e) 
    {
        System.err.println("Could not listen on port: 6666.");
        System.exit(1);
    }

    try 
    {
      client = host.accept();
    } 
    catch (IOException e) 
    {
        System.err.println("Accept failed.");
        System.exit(1);
    }
}

//close method
public static void Close () throws IOException
{
    out.close();
    client.close();
    host.close();
}


//main method
public static void main(String[] args) throws IOException 
{

    Host serverhost = new Host();
    input1=in.readUTF();

}
} 
  • 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-31T04:42:50+00:00Added an answer on May 31, 2026 at 4:42 am

    Your client will also need at least one extra thread so that it can receive and send chat messages at any time.

    On the server side, you will need to put the call to accept inside a loop in order to accept multiple connections. Then, for each newly connected client you can spawn a new thread and pass it the socket object obtained from accept:

    private class ClientThread implements Runnable {
        private Socket socket;
    
        public ClientThread(Socket socket) {
           this.socket = socket;
        }
    
        public void run() {
            // read messages from socket and send them to the other client.
        }
    }
    
    ...
    
    while(true) {
        try {
            client = host.accept();
            Thread t = new Thread(new ClientThread(client));
            t.start();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.