Solved

Java + socket Thread

Posted on 2006-06-20
6
501 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 32 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 31 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 31 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 31 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:
Suggested Courses

631 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