Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java + socket Thread

Posted on 2006-06-20
6
Medium Priority
?
511 Views
Last Modified: 2012-08-14
My question is why u hAVE to use thread in socketprogramming....?????

but when u send data over the socket..something is blocking the data and therefore u have use thread in socket i dont know really but i hope u cn answer me...

one of Asnwer is taht u can handle more than one client....


My second is why in java we use gui.thread...??
0
Comment
Question by:Maggieshah
6 Comments
 
LVL 5

Accepted Solution

by:
tbboyett earned 128 total points
ID: 16947497
The only reason I know of that you would use threads in socket programming is so that your server can start processing a request from one client and at the same time continue listening for other clients to make a request.

Maybe this page can help you understand a little better:
http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/socket.html
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 124 total points
ID: 16948749
That is correct. You need to do both the things parallelly - 1) keep listening for more client connections, 2) handle each client connection parallelly.

See the code sample I posted here to see how a multi-threaded server is developed:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21757720.html#16084277
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 124 total points
ID: 16949027
> My question is why u hAVE to use thread in socketprogramming....?????

You do not. Unless you want to support multiple clients (look at the comments above).

> but when u send data over the socket..something is blocking the data and therefore u have use thread in socket i dont know really but i hope u cn answer me...

When many requests go to the socket on the server all these requests are queued at the port (so the server accepts connections sequentially). In order to serve them all you create a new thread (on the server) for each client connection you accept. For more information: http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html

> My second is why in java we use gui.thread...??

There is no such package. I guess you mean why many times you need threads when you have GUI in the same programme? This is done because all the handling of the GUI functionallity (drawing, responding to user input etc) should be done in the event-dispatching thread and not in the main thread. for more information: http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
0
 
LVL 8

Assisted Solution

by:kaliyugkaarjun
kaliyugkaarjun earned 124 total points
ID: 16950409
> why u hAVE to use thread in socketprogramming....?????

I think u shud use java NIO...if u want to support multiple clients then threading may be required..but to avoid blocking of data..go for java nonblocking I/O

http://java.sun.com/j2se/1.4.2/docs/guide/nio/
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question