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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:29:32+00:00 2026-06-13T06:29:32+00:00

Alright, I’m trying to set a program I have to receive data from a

  • 0

Alright, I’m trying to set a program I have to receive data from a socket, and send data to a socket. I’m stumped on how to get the client side of the socket to send specific data, then have the server side send specific data. Here’s what I currently have, it’s only my server-side because I’m really lost on the client part as of now.

To further evaluate, I would like to do as listed in the following, yet I can’t figure out what to research to write the client-side of the socket, and if there was any code I would need to rewrite in the server side?
To further evaluate

package sockets;
import java.net.*;
import java.io.*;

public class SocketMain {
    private int port = 0;
    public ServerSocket socket;
    public Socket clientSock;

    public SocketMain() {
        init();
    }

    public static void main(String[] args) {
        new SocketMain();
    }

    private void init() {
        try { 
            socket = new ServerSocket(port);
            System.out.println("Server started, bound to port: "+port);
            clientSock = socket.accept();
            File directory = new File("./Storage/");
            if (!directory.exists()) {
                directory.mkdirs();
            }
            File file = new File(directory + "/Store.dat");
            if (!file.exists()) {
                file.createNewFile();
            }
            DataInputStream in = new DataInputStream(clientSock.getInputStream());  
            FileWriter fw = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fw);
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                bw.write(line+"\n");
                bw.flush();
                bw.close();
            }
            socket.close();
            clientSock.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
  • 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-06-13T06:29:34+00:00Added an answer on June 13, 2026 at 6:29 am

    About what you currently have:

    The first thing that jumped in my eye was this loop:

    while ((line = in.readLine()) != null) {
        System.out.println(line);
        bw.write(line+"\n");
        bw.flush();
        bw.close(); // <- Problem
    }
    

    You’re closing the writer every time you write a line. Now, as the documentation of Writer.close() states:

    Closes the stream, flushing it first. Once the stream has been
    closed, further write() or flush() invocations will cause an
    IOException to be thrown.
    Closing a previously closed stream has no
    effect.

    You should be getting IOExceptions for every line after the first one. You program will however not crash, since you’re catching the exceptions.


    Second, you use DataInputStream to read from your client, but write out using a BufferedWriter. As the former states in it’s documentation:

    A data input stream lets an application read primitive Java data types
    from an underlying input stream
    in a machine-independent way. An
    application uses a data output stream to write data that can later be
    read by a data input stream.

    The class includes multiple methods for boolean, char, int, whatever primitive data-type you can think of. But for the DataInputStream.readLine()-method, it clearly states:

    Deprecated. This method does not properly convert bytes to characters.
    As of JDK 1.1, the preferred way to read lines of text is via the
    BufferedReader.readLine() method.

    So, for reading strings, you should use a BufferedReader.


    About what you don’t yet have:

    The communication over sockets is established on a “ask-answer” base. The workflow should be something like this:

    1. Client opens connection
    2. Server accepts connection
    3. Client asks for something (using the server-sockets OutputStream)
    4. Server reads the request (using the client-sockets InputStream)
    5. Server answers (using the client-sockets OutputStream)
    6. Client reads the answer (using the server-sockets InputStream)
    7. Repeat steps 3-6 if necessary.
    8. Connection is closed (either by client, server or both).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, here I am again trying to write code from scratch and I can't
Alright, so here is the property I have public List<String> names{ get{ lock(_names) return
Alright so i have been working on this Dynamic load of a spinner from
Alright, here is my problem i'm trying to solve. I have an index page
Alright. So I have a very large amount of binary data (let's say, 10GB)
Alright, this one's got me seriously stumped, after trying things out for hours with
Alright I am trying to set up an OAuth Provider in PHP, but I
Alright, so here's what I'm trying to do. Let's say I have the following
Alright so I have no idea how to even begin doing this But basically
Alright I created some custom classes for my project sourced from this tutorial ,

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.