Solved

Java + socket Thread

Posted on 2006-06-20
6
462 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 32 total points
Comment Utility
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:mayankeagle
mayankeagle earned 31 total points
Comment Utility
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
Comment Utility
> 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
Comment Utility
> 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
topping3 challenge 14 48
wordmultiple challenge 12 89
Java - Why doesn't this JFrame work 3 14
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now