Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ServerSocket Problem  RSS feed

 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am trying to create a client to server chat program in which the users can communicate. I have successfully created the application so the users can communicate via. text (chatting) as well as using the microphone to communicate. Problem arises when I have two ServerSockets or even try to share the ServerSocket with both features (text & microphone) What will happen is the streams will close or if I have two separate ServerSockets open one will work and the other will not , But; what will happen if I turn one off the other ServerSocket will work fine until i turn the other back on in which it'll have problems again.


Can someone please explain to me whats going on? how do you run two serverSockets on the same application and allow them both to receive data? I can show my code if requested client is about 400 lines long with server...


 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two ServerSockets have to be listening on different ports, if you want them to both work at the same time. And they would have to do the data-receiving work in different threads.
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul for a prompt hand. OK, So they are on different ports exp. [1] severSocket 222 [2] serverSocket 222 lets say [1] is for text and [2] is for files etc. BUT they need to be most defiantly in separate threads??
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you could do the two transfers in the same thread, but why would you? The code to do that would be far more complicated and it would just duplicate what the operating system does for you in switching between two threads. Or rather, it would attempt to duplicate that but it wouldn't do nearly as good a job. The OS might run the two threads on separate cores, or it might switch away from one thread when it was waiting for data to arrive over the network. All things which would be difficult or impossible to replicate in Java.
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I am assuming the reason why one is not going through and getting a socket connection refusal is because of this I will make a new thread and report back.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, if your ServerSocket accepts a connection and then continues to handle the Socket which connected to it, then it won't respond to other connection attempts. That's why you need it to start a new thread to handle the connection.

I'm not sure what happens when you have two of them, but most applications only need one.

But every socket tutorial I've ever seen includes code which starts new threads for socket handling, and explains why it's done. You should consider reading some tutorials -- I know it's a useful learning experience to do everything from scratch but learning from tutorials shouldn't be rejected. Personally when I try to learn about a Java feature which I don't know anything about, I start from the tutorial and play around with its example code until it's doing what I want it to do.
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for the insight Paul, I will take your advice and watch some more tutorials... never can learn too much! So in all honesty should I just use the single ServerSocket? instead of using two? I mentioned above I believe that it closes the chat program as soon as the file sends and doesn't actually go through but this is because it should have been threaded so I am leaning towards just using one ServerSocket now and threading.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most servers listen on only one port. However if your design requires two then by all means use two.

Note that I said "your design". That doesn't mean you should use two just because you tried to use only one and you couldn't make the programming work. If you look around at servers which you are familiar with you'll see that they almost uniformly listen on only one port, and that's almost exclusively the default port for the protocol.
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info, I've tried to apply it but I'm still getting a refusal to connect. The method that actually listens to download the file is at the very bottom. I included just the server-side because I have tested the client side sending to another server that just listens for the incoming data and it works flawless its just this server side particularly this code whats giving me problems and from my understanding and knowledge i don't know what I'm doing wrong.


 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once the ServerSocket accepts a connection it should start a new thread to handle the connection. If it doesn't, then it won't be able to accept any more connections.
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im officially stumped I have 7 hours on this now and am still able to make little progress on this. i have rebuilt the whole server hoping I missed something, but apparently its me. I don't understand where I'm going wrong but the output is showing below... 130 is referring to Objectoutput.writeObject(hostName + ": " + m); I have tried correcting it with no prevail If anyone can help that would be amazing.. When I start the server up and then the client everything seems to go fine at first than I get the ERROR(S) I put below on serverSide




As well when activating the Microphone and sending the sound file I am receiving this Error which I don't understand entirely, but am pretty positive its related to the first problem as well its pointing to the client-side
message = (String) Objectinput.readObject();
















Im going to post my code with the server first following the client











CLIENT SIDE BELOW


 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable in question is assigned a value in the run() method, the one which is called by the Thread when you start it. However it's clear from your stack trace that you never start any threads, since the run() method isn't in the stack trace.

Look, you have way too much code now to be debugging things which you should have done at the very beginning. So put that code aside and start again. Write a simple server which does nothing but accept connection requests and start threads to handle them. A plain old echo server would be good enough. Then to test that you would need one server and two clients, to make sure that your server can actually handle more than one client.

Once you've got that working, then you can start putting back the code you've written so far. But when you do that, remember that it's the code in the thread you spawned which should be doing all of the communication with its client. You should probably put this code in a separate class, first of all because it's the right thing to do and second of all because then you won't be tempted to mix up request-accepting code with communication-handling code.
 
travis Haycock
Ranch Hand
Posts: 67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks Paul, I'm rebuilding now I did what you recommend; started small scale with a simple echo server which worked kind of showed more in depth actually when I was working with shorter code what was going wrong. I tried to make the server fail and have found a couple faults where I went wrong with the threads.
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where I think I was mistaken was just a lack of knowledge on my part I thought for example I could have used: (ill illustrate)


while(true){
Thread 1 (perform streams only etc.)

Thread 2 (perform only download etc.)



}


But in reality i need to replicate the whole class(s) for each connection .. if I'm not getting stuff mixed up again.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's part of the "Separation of Concerns" concept. Your code modules -- methods and classes -- should each have a single concern. They should be responsible for a single thing. So you need a SocketServer to receive and accept connections. That's the single concern of the SocketServer. You shouldn't encumber that code module with any other processing. No saying Hello, no file transfers, none of that. After it accepts a connection it should give that connection to some other object whose single concern is to manage communications with that connection.

Now "manage communications" is a sort of wishy-washy piece of terminology. What is that anyway? Okay, you're going to say we can send text messages and we can transfer files and... So already we see the word "and" which tells us that there's more than one concern going on there. So we're going to have to separate those concerns. Maybe that just involves having different methods to do different things -- you've been doing that -- or maybe it's necessary to have more than one class for each of them. It just takes a bit of practice to be able to make that decision. My feeling is that being the gatekeeper waiting for connections is significantly different than being the manager who talks to clients. So that's why I suggested a separate class. But receiving a text message and echoing it to other clients in a chat session, versus receiving a file and making it accessible to other clients, that seems not so different. So maybe your communication manager just needs different methods for those things.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!