Solved

Java + socket Thread

Posted on 2006-06-20
6
493 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp insert to database example 2 90
jsp CRUD operations with and without prepared statement also hibernatge 1 50
Java class and jar 3 61
Overriding a method 3 14
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

751 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