Connection works but program does not continue in the code ?
Hello,
I have a client, a server and a data class implementing Serializable interface.
Both classes communicate via sockets.
Between the sockets I transfer objects from the type of the data class.
The client and the server are running each in a Thread.
First the server is started then pressing a certain button in the GUI the client is started.
1. Why is the code in the clients Thread never going beyond this code line: System.out.println("test"); ???
2. Why is the file satz.dat not written ?
This is the part code of all 3 classes which is making me trouble:
Client code:
public class ClientThread extends Thread
ClientThread()
public void run()
try
InetAddress ip = InetAddress.getByName("localhost");
Socket socket = new Socket(ip , ServerThread.PORT);
System.out.println("test");
ObjectInputStream incomingObject = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream outgoingObject = new ObjectOutputStream(socket.getOutputStream());
outgoingObject.writeObject(serializeObjekt(meineBuchdaten)); // serialize the object "meineBuchdaten"
incomingObject.close();
outgoingObject.close();
socket.close();
catch (Exception e)
e.printStackTrace();
Method to serialize the string data of the data class called Buchdaten class:
public Object serializeObjekt(Object objekt) throws IOException
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("d:/satz.dat")));
oos.writeObject(objekt);
oos.flush();
oos.close();
return oos;
server class:
public class ServerThread extends Thread
public static final int PORT = 8080;
private ServerSocket myServerSocket = null;
private Socket myClientSocket;
private Database myDatabase = new Database();
public ServerThread()
public void run()
try
myServerSocket = new ServerSocket(PORT);
catch (IOException e)
e.printStackTrace();
System.out.println("Started: " + myServerSocket);
try
while(true)
// Warten until the client connects...
myClientSocket = myServerSocket.accept();
System.out.println("Connection done - handshake " + myClientSocket);
ObjectInputStream incomingObject = new ObjectInputStream(myClientSocket.getInputStream());
ObjectOutputStream outgoingObject = new ObjectOutputStream(myClientSocket.getOutputStream());
Buchdaten bd = (Buchdaten) incomingObject.readObject();
System.out.println("This should be the deserialized data: " + bd);
myClientSocket.close();
catch(Exception ex)
System.out.println(ex.getMessage());
data class:
public class Buchdaten implements Serializable
private static final long serialVersionUID = 1L;
private String autor;
private String titel;
private String jahr;
private String verlag;
private int number;
private int id;
public Buchdaten()
public void setDataToArray(String autor, String titel, String jahr, String verlag , int number)
this.autor = autor;
this.titel = titel;
this.jahr = jahr;
this.verlag = verlag;
this.number = number;
public void setDataToArray(int number , int id)
this.number = number;
this.id = id;
public void setDataToArray(String autor, String titel, String jahr, String verlag , int number , int id)
this.autor = autor;
this.titel = titel;
this.jahr = jahr;
this.verlag = verlag;
this.number = number;
this.id = id;
}
>
Oh, and by the way, is there a reason you aren't just using normal java RMI?
Edited by: jtahlborn on Feb 1, 2008 9:34 PMOh, and by the way, is there a reason you aren't just using normal java RMI?
yes for now i have to do it this way. The app must only run on my home pc but later i have to do it with RMI, but first it must work with sockets and streams etc stupd i know... ;-)
sabre150: quote:"As with all two way communication, one thread should be used for writing and another for reading. This ways the blocking nature of the streams works for and not against."
0: Does that mean i have to open 4 threads ? 2 threads for the client class and 2 threads for the server class? each one has an ObjectInput/Output - stream right?
For now i have only opened the outputstream on client side and the inputstream on server side to see wether it works at all. Furthermore my object is now serialized to the satz.dat file and it works.
1. Is there a way to serialize my data "meineBuchdaten" on-the-fly without writing it into a file on harddisk?
2. I want to print out the deserialized data but it doesnt work i get no output using the system.out.println method?
3. After this output: Connection done - handshake Socket[addr=/127.0.0.1,port=3139,localport=10001] I get this output: null
why null? from where does this null come?
Edit: ok my debugger work now again i reinstalled eclipse... debugging the cast into "meineBuchdaten" is the problem because right after this the debugger jumps into an exception this one
catch(Exception ex)
System.out.println(ex.getMessage());
}Edit: I have changed again the code a bit only this:
// Output of the deserialized data for test purposes
System.out.println("This should be the deserialized data: Autor: " + bd.getAutor());
its accessing the autor attribute variable of the bd object in a proper way but still the bd object is null i guess the problem is my serialized data on the client side is not transported what do i wrong can someone pls help me please ?
changed code:
client class:
public class ClientThread extends Thread
ClientThread()
public void run()
try
InetAddress ip = InetAddress.getByName("localhost");
Socket socket = new Socket(ip , ServerThread.PORT);
System.out.println("test");
// ObjectOutputStream for the object to be sent over socket to the server
ObjectOutputStream outgoingObject = new ObjectOutputStream(socket.getOutputStream());
// writing the class object "meineBuchdaten" into a file on the hard disk
try
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("d:/satz.dat")));
oos.writeObject(meineBuchdaten);
oos.flush();
oos.close();
catch (NotSerializableException nse)
System.out.println("No Serialization of the class " + nse.getLocalizedMessage() + " is possible");
catch(IOException ioe)
System.out.println(ioe.getMessage());
// closing the ObjectOutputStream and the client connection to free resources
outgoingObject.close();
socket.close();
catch (Exception e)
e.printStackTrace();
server class:
public class ServerThread extends Thread
public static final int PORT = 10001;
private ServerSocket myServerSocket = null;
private Socket myClientSocket;
private Database myDatabase = new Database();
public ServerThread()
public void run()
try
myServerSocket = new ServerSocket(PORT);
catch (IOException e)
e.printStackTrace();
System.out.println("Started: " + myServerSocket);
try
while(true)
// wait until the client connects...
myClientSocket = myServerSocket.accept();
System.out.println("Connection done - handshake " + myClientSocket);
ObjectInputStream incomingObject = new ObjectInputStream(myClientSocket.getInputStream());
// Reading the serialized data and cast it to the proper type
Buchdaten bd = (Buchdaten) incomingObject.readObject();
// Output of the deserialized data for test purposes
System.out.println("This should be the deserialized data: " + bd);
// closing the ObjectInputStream and the client connection to free resources
incomingObject.close();
myClientSocket.close();
catch(Exception ex)
System.out.println(ex.getMessage());
}Edited by: pel on Feb 2, 2008 2:04 AM
Similar Messages
-
Wireless connection works, but wired does not/gets yellow exclamation
Ive had this problem for over a month and cant seem to find a solution. ive googled around and tried every fix i could with no success. Ive also tried completely reformatting my hard drive, which also didnt work
Wifi and LAN using different device, if you already doing basic TS for networking even reformat OS,
Did you also try reinstall and update driver, isolate cable issue, ping local host or do clean boot?
http://windows.microsoft.com/en-us/windows/cant-connect-internet#1TC=windows-7
if still not solved means issue from device itself or consider HW issue -
How can I get my iPhone to show up on the panel to the right iTunes? I keep connecting it but it does not work.
http://support.apple.com/kb/TS1538
-
internet Download Manager cannot integration with firefox4 its Look in the menus it works but it does not actually work
IDM may need to be updated to work with Firefox 4.0. You could contact them and make them aware of the problems you are having with their program.
-
I cannot get my Acrobat Pro to open. I already uninstalled it and re installed it. But still does not open. The program pops to ask for permission to open the program, but it never actually opens. I trouble shoot it and it says incompatible application.
It sounds like an issue of trying to run XI on VISTA. That combination does not work. Info on both the Acrobat full version (including sub-version) and OS would help. It might also be an issue of running 11.0.0 on a system that needs the updates to work.
-
My iphone 4 does not register calls made to it. if i make a call from another phone, i can hear it ringing, but it does not register on the iphone. also, i do not get any messages when this happens. then after a day or two it decides to start working.
Clean iPhone charging port with clean dry toothbrush. See if better. If still problem clean charging port again with toothbrush and small amount of Isopropyl Alcohol.
-
Hello! I cannot download applications, when I try to do it the application says "waiting" but it does not continue downloading! Can someone help me????
Download only one application at a time.
Click all downloading applications until they all say "Paused". Then click one at a time, let it complete downloading, then click the next and repeat. -
My Wi-Fi connection on my iMac drops frequently. The other computers in the house do not have this problem. If I go into the Network Preferences and choose my Network it connects immediately but it does not remember the setting and I must reselect my network frequently.
The iMac picks up a wireless signal from the back of the screen, so you would want to avoid having it close to a wall, or in a confined area where the wireless signal cannot easily reach the iMac's antenna.
We don't know how far the iMac is from your wireless router, or how many walls or other obstructions might be in the signal path that would limit the quality of signal that the iMac receives.
Nor do we know whether you might have a cordless phone near the iMac, or whether you may be using a wireless keyboard and/or mouse....all of which can interfere with the wireless signal.
Can you provide some more info on this? -
I had put my 3G sim in one IPAD 2 for testing. After 2 days I put the sim in my own IPAD 2 , but it does not accept/read the sim. Kindly advise ???
Hello Murali19,
I would recommend following through the article linked below for some useful troubleshooting steps that may help get Personal Hotspot enabled with the desired SIM card.
iOS: Troubleshooting Personal Hotspot
http://support.apple.com/kb/TS2756
Cheers,
Allen -
I am trying to authorize my computer, I have followed the on screen instructions but it does not give me the option to authorize.
Please help!Always remember to 'de-authorize' you computer when selling or sending for service. If sending for repairs you might not get the same mother board or HD back and some how this counts as an authorized computer against your count.
MJ -
just bought "the Great Gatsby" at iTunes but it does not play, all the other movies are playing - any ideas
Hello cf-design,
You may need to delete, then redownload the movie.
How to delete content you've downloaded from the iTunes Store, App Store, iBooks Store, or Mac App Store
http://support.apple.com/kb/HT5772
Downloading past purchases from the iTunes Store, App Store, and iBooks Store
http://support.apple.com/kb/HT2519
If you're still unable to watch the movie, please report the issue to the folks in the iTunes Store.
How to report an issue with your iTunes Store, App Store, Mac App Store, or iBooks Store purchase
http://support.apple.com/kb/HT1933
Cheers,
Allen -
I have installed apps Hungry Shark vobec but it does not show me the settings I like it or not even in the game center I have not updated results from it you can not help me thank you
Hi Marcel1no,
Welcome to the Apple Support Communities!
I understand that it can be frustrating when an application downloaded from the App Store is not behaving as you would expect. In this situation, I would recommend using the attached article for information on troubleshooting apps downloaded from the App Store.
iOS: An app you installed unexpectedly quits, stops responding, or won’t open - Apple Support
Cheers,
Joe -
When I connect to itunes it does not appear in the devices..please help me ........how...its URGENT...
Here:
iOS: Device not recognized in iTunes for Windows
or
iPhone, iPad, iPod touch: Device not recognized in iTunes for Mac OS X -
I have a new 7th gen iPod, I have up loaded the latest iTunes version, I go to the music tab to sync, I click sync and the ipod starts to sync, but it does not sync all the music. It indicates all the songs on the screen, but not on the ipod
I do not have the screen shown with the
Pull down the “Store” menu and choose “Deauthorize This Computer”
3. Now go back to the Store menu and choose “Authorize This Computer”
options. -
Hello buy a movie on apple tv, and now can not find the device tells me I should go to settings / downloads / check downloads, but that does not exist in the menu of the apple tv, please help
Welcome to the Apple community.
A number of people have reported similar issues recently, many appear to have found a discrepancy of some kind in their account settings and making changes to these settings or simply resetting them has resolved the matter.
Check your account details are correct, check that you are using the correct details for Location, Region and Time Zone. You may want to change these settings and then change them back to see if that helps.
You might also try logging out of and then back into your iTunes account (Settings > iTunes Store > Apple ID’s > Sign Out) and restarting your router. (Some have reported that after doing this, they need to check for rentals even though it may report that there aren’t any, after which rentals once again appear in the ‘movies’ feature)
Rentals should appear under their own heading in the ‘Movies’ feature, you might also check at Settings > iTunes Store > Check For Rentals.
Maybe you are looking for
-
How to open each CS version file in its proper version program when double-clicked?
Hello everyone, This one is about the coexistence of Indesign CS4 and CS5. In my company: I'm working with Indesign CS4, but a colleague is testing CS5 in real day-to-day works, and everyday I come across CS5 files that I need to correct, so I have C
-
Call taking forever to load when I answer, anyone have this problem?
When I receive a phone call, I push the green button to accept the call and normally it would go into the active call screen which shows the buttons: mute, keypad, speaker etc. but lately it takes forever to switch to that screen. It'll stay on the c
-
Duplicate ( same ) condition type in Purchase Orders
Hi, by mistake in the purchase order users are entering same condtion type more than one time for the same item. business wants to stop this. so please guide me on this. Thanks a lot h shah
-
When you publish a webpart what should the debugging server be set to?
Hi, I am trying to deploy a webpart in Visual Studio. First of all, it says which local site do you want to use for debugging? Should this be a SharePoint server on your local machine that you are using VS on which will be different from the remote s
-
Etre check results...."killed" entries
I have entries that say "killed" how do I fix that? EtreCheck version: 2.1.7 (114) Report generated January 31, 2015 at 2:11:35 PM EST Download EtreCheck from http://etresoft.com/etrecheck Click the [Support] links for help with non-Apple products.