Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  objectinputstream and objectoutputstream problem  (Read 1047 times)
0 Members and 1 Guest are viewing this topic.
Offline TriSwords

Junior Newbie





« Posted 2005-07-03 18:09:38 »

Ok I have been making a little card game, and have started to implement a client/server idea into it.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
private void recieveData() throws IOException {
try {
rec = (Packet) input.readObject();
} catch (ClassNotFoundException e) {
System.out.println("Object not a packet");
} catch (SocketException e) {
System.out.println("Socket lost");
//Golden T Game Engine Stuff
parent.nextGameID = Start.TITLE;
finish();
}
}


That is my recieve code (works same for my client and server). rec is a Packet class defined already.

1  
2  
3  
4  
5  
6  
7  
8  
private void sendData(Packet p) {
try {
output.writeObject(p );
output.flush();
} catch(IOException ioException) {
System.out.println("Error writing message");
}
}


My Send data code also, it also works the same for my client and server. My streams and connection are working fine. In fact, if I send just a standard string, and not a Packet(my basic class for sending info across the network) and set it to recieve a string(not a packet), it works perfectly fine. But as soon as I use a packet, it gets to the line

rec = (Packet) input.readObject();

in recieveData() and stops running. My packet class looks just like

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
//Import from Java 1.5
import java.io.Serializable;

//Packet class to send across the network, must be Serializable to be sent over
//network
public class Packet implements Serializable{

//The variables the packet can send
private String message = "";

//Set the message that will be sent
public void setMessage(String aMessage) {
message = aMessage;
}

//Get the message sent over the network
public String getMessage() {
return message;
}

//Write the class as a string
public String toString() {
return message;
}

}


It implements Serializable, so I cannot figure out the problem. When it hits the line, it does not crash, it just stops at that point. If I take that line out, then it runs fine, and continues on through the whole program, just without receiving data.

I thought maybe I set it up wrong, but everything works to perfection if I use a String and not a Packet like I said before. Anyone have any clue on to why this problem is occuring. It is using a Socket, ObjectInputStream, and ObjectOutputstream for my client(they are all initalized correctly), and a ServerSocket, Socket, ObjectInputStream, and ObjectOutputstream for my server(they also are initalized correctly).

Please Help. Thanks.
Offline princec

JGO Kernel


Medals: 378
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2005-07-03 21:52:20 »

Try adding an output.reset() after the flush.

Cas Smiley

Offline TriSwords

Junior Newbie





« Reply #2 - Posted 2005-07-04 01:37:55 »

hmm didnt change it. I did however manage to get it working by extending a thread. Then I put the recieveData() into the run method. Seems to be working right now.

Thanks for the help
TS
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline aaronk42

Junior Newbie





« Reply #3 - Posted 2005-08-07 16:36:25 »

Yes...when you're receiving data over the network, the process is blocked. That means that your program will sit and wait at the inputObject() line until it receives data, and then it will continue.

You were right in your solution, though...putting your networking code in a new thread will simulate a non-blocking read.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (22 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (62 views)
2014-09-10 13:57:51

TehJavaDev (88 views)
2014-09-10 06:39:09

Tekkerue (44 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (47 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!