Computer Science/Sockets

Advertisement


Question
Hi i asked you a question a little bit ago and you thought it was a homework question, but it wasn't. I'm wanting to learn how to do network programming on my own, and the site i was looking at was here http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/socket.html if you go down you'll see i just cut an example out. I guess i should have told you it wasn't homework, but I'll repost my question below.


I'm trying to learn java networking and i'm reading up on multithreaded servers. Here is the source code I'm looking at:


public void listenSocket(){

try{

server = new ServerSocket(4444);

} catch (IOException e) {

System.out.println("Could not listen on port 4444");

System.exit(-1);

}

while(true){

ClientWorker w;

try{

//server.accept returns a client connection

w = new ClientWorker(server.accept(), textArea);

Thread t = new Thread(w);

t.start();

} catch (IOException e) {

System.out.println("Accept failed: 4444");

System.exit(-1);

}

}

}



I understand how the code above works, but I'm having problems understanding how each thread receives messages from the different clients. I thought only one program can be on a socket at a time. How does each thread receive the correct message from the many clients that could be communicating with the server since they're all connected to port 4444? Also what part of the operating system manages the ports; what's it called? Thank you for your help.

Answer
Jeremy:

My appologies for not writing back sooner, but it is the Holidays.  Think of sockets as ports: When once socket on one side connects to the other side it is given a unique socket id: this is relayed back to the client from the server.  When the socket closes or in most cases gets orphaned the next time the client connects to the server they are given new number. This is all done via the client connection part of the code.

Have a Happy New Year and I hope Santa was good to you!

Kind Regards,
Jeff  

Computer Science

All Answers


Answers by Expert:


Ask Experts

Volunteer


Jeff Laatsch

Expertise

I can answer programming questions related to: C, C++, PHP, PERL, VB, SQL, ASP, JAVA, JS, XHTML, XML, CSS, PL/SQL, AWK, SED, VI Editor and if I don`t know the answer I have 20+ years systems experience as my resource. I can handle general questions in most areas of computer science including systems integration, API, networking related issues.

Experience

An accomplished WEB Master/Developer, Programmer Analyst, Systems Administrator with 12+ years of industry background in full system management and technical hardware/software support expertise including SUN, HP-UX, AIX, LINUX, and Windows NT,Win2000, 2003 with Active Directory: Well rounded in software engineering principles, requirements gathering, analyst, definition, system architectural design, OOA, OOD, UML, SEI-CMM and SDLC: Knowledgeable in Data Warehouse, Data Mart technologies, and experienced working with ORACLE databases utilizing ERP, SQL, PHP, PERL, C, C++, PL/SQL,SQL, JAVA, VB.NET, SOAP and other Dynamic WEB technologies: Analytical thinker and problem solver: Goal oriented professional who has a Masters in Computer Systems with emphasis in computer programming (C++ and Application Development).

Education/Credentials
City University, WA 12/03
Master of Science in Computer Systems
with Emphasis in Computer programming (C++/App Development)
Chapman University, WA 6/01
Bachelor of Science in Computer Information Systems
Emphasis in Computer networks and security

20+ years of experience in the Computer Science Industry

Awards and Honors
7/96,9/96,10/96,1/97,2/98,6/99,10/01,7/02,01/03,
Outstanding Achievement & Special Recognition

©2012 About.com, a part of The New York Times Company. All rights reserved.