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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:39:55+00:00 2026-05-30T20:39:55+00:00

im wondering if there are two programs, one called server and the other called

  • 0

im wondering if there are two programs, one called server and the other called client and these two are illustrating a server and client respectively, is it possible to test them in one machine? considering i defined the client socket as follow:

socket = new Socket("127.0.0.1",3000);

Edit:
Server part:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

class TCPServer
{
       private static ServerSocket serverSocket;
        private static Socket socket;
        private static PrintWriter toClient;
        private static BufferedReader fromClient;
        public static void run() throws IOException
            {
            System.out.println("Server is waiting for connections...");
            while (true)
            {
                openStreams();
                processClient();
                closeStreams();
            }

        }

        public static void openStreams() throws IOException
            {
            serverSocket = new ServerSocket(3000);
            socket = serverSocket.accept();
            toClient = new PrintWriter(socket.getOutputStream(),true);
            fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        }
        public static void closeStreams() throws IOException
            {
            fromClient.close();
            toClient.close();
            socket.close();
            serverSocket.close();
        }
      public static void processClient()throws IOException
{
    String msg = null;
    do {
        msg = fromClient.readLine();
        toClient.println("Client said " + msg);
    } while (!"exit".equals(msg));
}

        }

client part :

package clientserver.pkg1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

class TCPClient
{
      private PrintWriter toServer;
            private BufferedReader fromServer;
            private Socket socket;
            public void Client( )throws IOException
                {
                socket = new Socket("127.0.0.1",3000);
            }
            public void openStreams() throws IOException
         {

                toServer = new PrintWriter(socket.getOutputStream(),true);
                fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            }
            public void closeStreams() throws IOException
                {
                fromServer.close();
                toServer.close();
                socket.close();
            }
          public void run()throws IOException
{
    openStreams();
    String msg = "";
    Scanner scanner = new Scanner(System.in);
    while (!"exit".equals(msg))
    {
        System.out.print(">");
        msg = scanner.nextLine();
        toServer.println(msg);
        String tmp = fromServer.readLine();
        System.out.println("Server said: " + tmp);
    }
    closeStreams();
}

            }

main class

public static void main(String[] args) {

       try
            {
                 TCPServer server = new TCPServer() {};
                 TCPClient client = new TCPClient();
                 client.run();
                 server.run();




            }
            catch(IOException e)
            {
                 System.err.println("Error in Client " + e.getMessage());
            }




java.lang.NullPointerException

1

toServer = new PrintWriter(socket.getOutputStream(),true);

2

      public void run()throws IOException
{
    openStreams();
.....
}

3

 public static void main(String[] args) {

       try
            {
                 TCPClient client = new TCPClient();
                 client.run();
            }

at client.run();

  • 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-30T20:39:57+00:00Added an answer on May 30, 2026 at 8:39 pm

    You certainly can, just have the client and server connected to the same port number.

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

Sidebar

Related Questions

There are two things that seem to be popular nowadays and I was wondering
I was just wondering if there is any difference between the two different new
I was wondering if there is a way to simplify this down from two
I'm wondering if these two expressions are equivalent, because if they are, it would
I was wondering which of these two scenario's works best for swapping between 2
I was wondering about the performance difference between these two scenarios and what could
I use two email programs, gmail and entourage (by Microsoft). I was wondering how
Wondering if there is any Text to Speech software available as a plug in
Wondering if there's any not-too-hard way to edit non-form text in html 4. I
Wondering if there is any way to get the lambda expressions that result from

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.