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

  • Home
  • SEARCH
  • 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 8950183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:23:44+00:00 2026-06-15T13:23:44+00:00

i’m making a program/game that will update automatically. i have the update part down,

  • 0

i’m making a program/game that will update automatically. i have the update part down, but not the checking of the version. i would have thought that it’d be pretty easy. heres what i’ve done. i wrote an updater for the game, and i wrote a server. the server starts a thread every time a client/updater connects. the thread handles everything. the game updater reads a file called version.txt and that provides the version number (default 0.0.1) and sends it to the server. the server does recieve the version, and will System.out.println(); if the version matches, and if i change the version, it changes the output. so that part works. but that is as far as it goes. the second part of the process is that the server then sends just a text file called NPS Game.txt (it sends anything, but txt was easy to test with) and the client replaces the old version of this file with the new one that just sent. the problem is that i keep getting an error that says the Socket is closed. i’ve tried using socket.setKeepAlive(true); but that didnt change anything (i put that on both the client and the server). here is the code:

server:

package main;

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

import javax.swing.JOptionPane;

public class Server {
static ServerSocket serverSocket = null;
static Socket clientSocket = null;
static boolean listening = true;

public static void main(String[] args) throws IOException {
    try {
        serverSocket = new ServerSocket(6987);
    } catch (IOException e) {
        ServerThread.showmsg("Could not use port: 6987");
        System.exit(-1);
    }

    ServerThread.showmsg("server- initialized");
    ServerThread.showmsg("server- waiting...");

    while (listening)
        new ServerThread(serverSocket.accept()).start();
}
}

server thread:

package main;

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

import javax.swing.JOptionPane;

public class ServerThread extends Thread {
Socket socket;
ObjectInputStream in;
ObjectOutputStream out;
String version = "0.0.1";

public ServerThread(Socket socket) {
    super("Server Thread");
    this.socket = socket;
}

public void run() {
    showmsg("server- Accepted connection : " + socket);
    getVersion();
    sendFile();
}

public void getVersion() {
    try {
        ObjectInputStream ois = new ObjectInputStream(
                socket.getInputStream());
        try {
            String s = (String) ois.readObject();
            if (s.equals(version)) {
                System.out.println("server- matched version :)");
            } else {
                System.out.println("server- didnt match version :(");
                System.exit(0);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        ois.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void sendFile() {
    // sendfile
    File myFile = new File("C:\\Programming\\NPS\\Files\\bin\\NPS Game.txt");
    byte[] mybytearray = new byte[(int) myFile.length()];
    FileInputStream fis;
    try {
        fis = new FileInputStream(myFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(mybytearray, 0, mybytearray.length);
        OutputStream os = socket.getOutputStream();
        showmsg("server- Sending...");
        os.write(mybytearray, 0, mybytearray.length);
        os.flush();
        socket.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void showmsg(String s) {
    JOptionPane.showMessageDialog(null, s);
}
}

and the client/updater:

package main;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;

import org.omg.CORBA.portable.InputStream;

public class Connections {
String IP, port;
String message = "";
Socket socket;

public Connections(boolean server, boolean updating, String IP, String port) {
    this.IP = IP;
    this.port = port;
    try {
        socket = new Socket(IP, Integer.parseInt(port));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (!server) {
        if (updating) {
            try {
                sendVersion();
                updating();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            client();
        }
    }
    if (server) {

    }
}

public void sendVersion() throws IOException {

    FileReader fileReader = new FileReader(
            "C:\\Program Files\\AVTECH\\NPS\\Files\\bin\\version.txt");
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    String stringRead = bufferedReader.readLine();

    bufferedReader.close();

    ObjectOutputStream oos = new ObjectOutputStream(
            socket.getOutputStream());
    oos.writeObject(stringRead);
    oos.flush();
    oos.close();
}

public void updating() throws IOException {
    int filesize = 6022386; // filesize temporary hardcoded

    int bytesRead;
    int current = 0;

    showmsg("client- connected");

    // receive file
    byte[] byteArray = new byte[filesize];
    java.io.InputStream inStream = socket.getInputStream();
    FileOutputStream fileOutStream = new FileOutputStream(
            "C:\\Program Files\\AVTECH\\NPS\\Files\\bin\\NPS Game.txt");
    BufferedOutputStream buffOutStream = new BufferedOutputStream(
            fileOutStream);
    bytesRead = inStream.read(byteArray, 0, byteArray.length);
    current = bytesRead;

    do {
        bytesRead = inStream.read(byteArray, current,
                (byteArray.length - current));
        if (bytesRead >= 0)
            current += bytesRead;
    } while (bytesRead > -1);

    buffOutStream.write(byteArray, 0, current);
    buffOutStream.flush();
    buffOutStream.close();
    inStream.close();
    socket.close();
}

public static void showmsg(String s) {
    JOptionPane.showMessageDialog(null, s);
}
}

i dont know what’s wrong with it, but it is really frusturating. if anyone can help, it would be appreciated. some things ive done: google all kinds of questions, tried implementing socket.setKeepAlive(true);. also, i thought it might be of note, in the server thread, right above the line BufferedInputStream bis = new BufferedInputStream(fis);, i put System.out.println(socket.isClosed); and it returned true. thats all i have. thanks in advance!

  • 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-15T13:23:46+00:00Added an answer on June 15, 2026 at 1:23 pm

    I think that closing one of both streams, closes the socket. So try to remove the ois.close() call out of your getVersion() method at the server side. Also get rid of the oos.close() call in your sendVersion() method at the client side.

    When you construct an ObjectOutputStream or ObjectInputStream and you are done with it, you shouldn’t close that stream, because it will close the underlying stream, which is in your case the socket.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.