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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:34:46+00:00 2026-06-14T05:34:46+00:00

I’m creating simple peer to peer game. Currently I’m working on Server/Client(Node) side. I

  • 0

I’m creating simple peer to peer game. Currently I’m working on Server/Client(Node) side. I occured strange problem, I can create ObjectOutputStream from socket, but program stops at creation of ObjectInputStream. To store sockets and create streams I’m using my own class(code below). What I’m doing wrong ?

public void run() {
    try {
        while (this.listen) {

            temp = serverSocket.accept();
            sockList.addSocket(temp);
            sockList.addObjOutStrm(temp);
            sockList.addObjInStrm(temp); // <------------------------ program stops


            setChanged();
            notifyObservers(temp);

            System.out.println("SERVER: Dodalem uzytkownika, oto pelna lista:\n\n"
                            + sockList + "\n");

            synchronized (sockList) {
                for (ObjectOutputStream oos : sockList.getOOSList()) {
                    oos.writeObject(sockList.extractToString());
                }
            }
        }
    } catch (IOException e) {
        System.err.println("SERVER: Błąd I/O serwera podczas nasluchu");
        //e.printStackTrace();
    }
}

My Own class to store sockets and streams:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SocketList {
    private List<ObjectOutputStream> obOutStrList;
    private List<ObjectInputStream> obInStrList;
    private List<Socket> sockList;

    private int maxUsers;
    private int currentUsers;

    public SocketList() {
        obOutStrList = Collections
                .synchronizedList(new ArrayList<ObjectOutputStream>());
        obInStrList = Collections
                .synchronizedList(new ArrayList<ObjectInputStream>());
        sockList = Collections.synchronizedList(new ArrayList<Socket>());

        maxUsers = 4;
        currentUsers = 0;
    }

    synchronized public void addSocket(Socket sock) {
        if (sockList.size() < 4) {
            sockList.add(sock);
            currentUsers++;
        } else {
            System.out.println("sockList: Blad max uzytkownikow");
        }
    }

    synchronized public void addObjOutStrm(Socket sock) {
        try {
            obOutStrList.add(new ObjectOutputStream(sock.getOutputStream()));
        } catch (IOException e) {
            System.out.println("Problem z uzyskaniem strumienia wyjsciowego dla " + sock.getInetAddress().getHostAddress().toString());
            e.printStackTrace();
        }
    }

    synchronized public void addObjInStrm(Socket sock) {
        try {
            obInStrList.add(new ObjectInputStream(sock.getInputStream()));
        } catch (IOException e) {
            System.out.println("Problem z uzyskaniem strumienia wejsciowego dla " + sock.getInetAddress().getHostAddress().toString());
            e.printStackTrace();
        }
    }

    synchronized public void removeSock(Socket s) {
        if (sockList.contains(s)) {
            System.out.println("SOCKETLIST: Usuwam " + s.toString());
            sockList.remove((Socket) s);
        }
    }

    synchronized public List<Socket> getSockList() {
        return sockList;
    }

    synchronized public List<ObjectOutputStream> getOOSList() {
        return obOutStrList;
    }

    synchronized public List<ObjectInputStream> getOISList() {
        return obInStrList;
    }

    public String toString() {
        return sockList.toString();
    }

    public String[] extractToString() {
        String[] retArr = new String[currentUsers];

        for (int i = 0; i < sockList.size(); i++)
            retArr[i] = sockList.get(i).getInetAddress().getHostAddress()
                    .toString();

        return retArr;
    }

}
  • 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-14T05:34:48+00:00Added an answer on June 14, 2026 at 5:34 am

    Are you sure, you want to do this? Reading an writing objects is not very portable, use some kind of protocol with a data structure like XML or JSON for communication. This is far more portable.

    To your problem:

    new ObjectInputStream(sock.getInputStream())
    

    will block and wait until the header has been written to the stream, as the JavaDoc states:

    This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

    Overall, I strongly suggest you a different architecture, use multi-threading to handle the client connections on the server, possibly using a thread pool and maybe even message queues. See package java.util.concurrent for all you need for this.

    Have a look here, for a start: http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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 use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.