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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:07:12+00:00 2026-05-22T13:07:12+00:00

I’m writing my first socket project and I have a problem. When the client

  • 0

I’m writing my first socket project and I have a problem. When the client sends a String to the server, the server can’t get this message.

public class ClientActivity {

    public static final String serverIP = "127.0.0.1";
        private Socket clientSocket = null;
        private static BufferedOutputStream out = null;

    public static void main(String[] args) throws IOException {
        ClientActivity me = new ClientActivity();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        out.write("Some string");
    }

    public ClientActivity() throws UnknownHostException, IOException {
           clientSocket = new Socket(serverIP, serverPort);
           out = new BufferedOutputStream(clientSocket.getOutputStream());
    }
}

Here is the server’s code:

public class DataTransferServer {

    private ServerSocket serverSocket;
    private ArrayList<DataReaderThread> workingThreads;
    private Boolean isListening;
    private Thread listener;

    public static void main(String[] args) throws IOException {
        new DataTransferServer(IProtocolConstants.SERVER_PORT).startServer(); // FIXME
    }

    public DataTransferServer (int port) throws IOException {
        System.out.println("Launch");
        serverSocket = new ServerSocket(port);
        workingThreads = new ArrayList<DataReaderThread>();
        isListening = true;
        listener = new ConnectionListener();
    }

    public void startServer() throws IOException {
        listener.start();
    }

    private class ConnectionListener extends Thread {
        @Override
        public void run() {
            DataReaderThread newThread  = null;
            while (isListening) {
                try {
                    newThread = new DataReaderThread(serverSocket.accept(), DataTransferServer.this);
                    newThread.start();
                    workingThreads.add(newThread);
                    System.out.println("! thread started");
                } catch (IOException e) {
                    e.printStackTrace(); // FIXME
                }
            }
            if (newThread != null) {
                newThread.interrupt();
                newThread = null;
            }
        }
    }


}

And here is the listener:

public class DataReaderThread extends Thread {
    private Socket socket = null;
    private BufferedInputStream in = null;
    private byte[] stuffBytes = null;

    public DataReaderThread(Socket socket) throws IOException {
        super("DataReaderThread");
        this.socket = socket;
        in = new BufferedInputStream(socket.getInputStream());
stuffBytes = new byte[10];
    }

    public void run() {
        try {
            while (in.read(stuffBytes, 0, stuffBytes.length) != -1) {
System.out.println("Received data:" + new String(stuffBytes));
            }
            in.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Socket getSocket() {
        return socket;
    }
  • 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-22T13:07:12+00:00Added an answer on May 22, 2026 at 1:07 pm

    I guess the BufferedOutputStream out does not send a TCP packet until the buffer is full.

    Try calling flush after write to send the data immediately:

    out.write("Some string");
    out.flush();
    

    You may also consider using the PrintWriter class instead, which has an autoflush mechanism.

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
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
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.