Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java + socket Thread

Posted on 2006-06-20
6
Medium Priority
?
507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

670 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