473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sockets and Synchronization

63
Okay, so I'm tying to get use to programming with sockets, and I have run into a bit of a snag.


As of now, my program basically works like this:

Server: Listen for a connection
Client: Request a connection
Server: Accept connection
Client: Send message
Client: Wait for response
Server: Processes message
Server: Send response

I want my client to also listen for message from the server. And these message can happen at any time. When you do something like:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. public final void listen(Socket s)
  3. {
  4.     InputStream in = s.getInputStream();
  5.     int aByte;
  6.     while((aByte=in.read())!=-1)
  7.     {
  8.         //Do stuff
  9.     }
  10. }
  11. ...
  12.  
The read blocks until until something can be read. This is a desired effect. However, I would like to prevent the listening thread from waking so that I can send a message and get the response without that while loop picking it up.

Is there any way to guarantee that?

I could open up two connections, one for sending and one for receiving. Is that frowned upon? It seems like a wast of resources.
Aug 21 '09 #1
2 5566
JosAH
11,448 Expert 8TB
A socket is not a verty heavy weight resource; open two sockets: one for the server to push messages to a client and one for normal client/server communication. The client should have a separate thread reading from the first socket (it'll block when the server doesn't send anything).

kind regards,

Jos
Aug 23 '09 #2
iLL
63
I think I'll give that a shot.

I got it working by blocking threads... But it caused communication to be about 20% slower.

Thanks for the reply.
Aug 23 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: user | last post by:
Hello I have a thread1 in which i am in infinite loop reading data from socket. But sometimes i want to transmit data using that socket (thread2 decides about it). Can i simply from thread2 call:...
4
by: scott | last post by:
hi all, Thx to any one that can offer me help, it will be much appreciated. iv got a multithreaded program and need to use thread synchronization. The synchronization does not have to...
0
by: Rod | last post by:
I orginally posted this to microsoft.public.sqlserver.ce but had not received any responses. I have a CF.NET application (C#) with a SqlCE database. We had originally planned to use SQL...
12
by: emma_middlebrook | last post by:
Hi Say you had N threads doing some jobs (not from a shared queue or anything like that, they each know how to do their own set of jobs in a self-contained way). How can you coordinate them so...
2
by: a_agaga | last post by:
Do you know are there some reasons why many do not make processes to communicate through memory? Why network connections (sockets) are used so commonly in IPC (inter process communication)...
0
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing...
2
by: Fernando | last post by:
Is there anything or anyway to do something simmilar to WSAAsyncSelect which is asyncronous but the notifications comes in the same thread( throgh messages ) , which for simple things facilitates...
13
by: Alexander Gnauck | last post by:
Hello, while using async sockets I ran into a strange problem which occurs only on some machines. I wrote a small demo application which can be used to reproduce the problem. You can download it...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.