Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes ServerSocket and thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ServerSocket and thread" Watch "ServerSocket and thread" New topic

ServerSocket and thread

A Agr
Ranch Hand

Joined: Feb 10, 2009
Posts: 32
I have 4 ServerSockets running at say port 1,2 ,3 4

A ServerSocket at port 1 listens for incoming connection from different clients and read from and write to clients. A socket connection can be created at any ServerSocket to write at other ServerSocket

Main class x creates serversocket and passes it to thread and waits for client connection and if got any , processes it. Along with this at same while(true){}loop next to it in class x also executes.Both should work simultaneously

When I run it, I get some problems

1. After a while I get Connection refused exception/// like it works for say 1 min and starts throwing exception
2. while true() loop is also continously writing to ServerSocket at say port 2. Output at port 2 becomes..print java..print java..
but i want it work like print jsp print java print jsp print java
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

This question does not have anything to do with SCJP. A moderator will move it shortly

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Vijitha Kumara

Joined: Mar 24, 2008
Posts: 3900

I'm not sure what you are trying to do here.. But "Socket l = p.accept();" is blocked until client made a connection to that particular port the ServerSocket is created to listen. How are you spawning new thread(s)? Can't see that in your code.

[How to ask questions] [Twitter]
I agree. Here's the link:
subject: ServerSocket and thread
It's not a secret anymore!