473,738 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deadlock situation while reading from sockets

Hi,

I am implementing a server that reads for socket connections at a
port, and processes the socket on a separate thread. We do not control
the client implementation. Here is the scenario.

0. Client connects to the socket.
1. Client writes to the socket.
2. Server reads from the socket.
3. Client waits for the response on the socket
4. Server writes to the socket.
5. Socket is closed.

Incoming request on the socket is not guaranteed to be terminated with
any particular sequence of characters. How does the server know how
many bytes to read, and when to stop reading?

Here are couple of things I tried.

1. readln on a buffered reader on the server side would block till it
receives the "eol" character.
2. I have tried reading x number of characters at a time, and thought
if number of characters read are less than the attempted number of
characters to read (or the read method returns -1) then read all the
data sent by the client. This works if the attempted characters to
read initially, are greater than the number of characters sent by the
client, but not if it is the other way.

I have used BufferedReader, and the PrintWriter classes.

I appreciate your help in resolving this issue.

Thanks.
Jul 17 '05 #1
9 5979
When the client connects to the socket it only writes the one time then
waits, so you can use the BufferedReader to read all of the data on the
socket. Each communication from the client has a new socket, so you
don't have to maintain a connection and wonder how much data is being
sent. When the socket is flushed to the server, it will append the eof
to the stream. Hopes this helps at all.

-C

javastudent wrote:
Hi,

I am implementing a server that reads for socket connections at a
port, and processes the socket on a separate thread. We do not control
the client implementation. Here is the scenario.

0. Client connects to the socket.
1. Client writes to the socket.
2. Server reads from the socket.
3. Client waits for the response on the socket
4. Server writes to the socket.
5. Socket is closed.

Incoming request on the socket is not guaranteed to be terminated with
any particular sequence of characters. How does the server know how
many bytes to read, and when to stop reading?

Here are couple of things I tried.

1. readln on a buffered reader on the server side would block till it
receives the "eol" character.
2. I have tried reading x number of characters at a time, and thought
if number of characters read are less than the attempted number of
characters to read (or the read method returns -1) then read all the
data sent by the client. This works if the attempted characters to
read initially, are greater than the number of characters sent by the
client, but not if it is the other way.

I have used BufferedReader, and the PrintWriter classes.

I appreciate your help in resolving this issue.

Thanks.


Jul 17 '05 #2
Chris Rohr <cj****@verizon .net> wrote in message news:<OA******* **********@nwrd dc03.gnilink.ne t>...
When the client connects to the socket it only writes the one time then
waits, so you can use the BufferedReader to read all of the data on the
socket. Each communication from the client has a new socket, so you
don't have to maintain a connection and wonder how much data is being
sent. When the socket is flushed to the server, it will append the eof
to the stream. Hopes this helps at all.
Thanks for your reply. I really appreciate it.

What method in the BufferedReader class can I use to read all the data
on the socket? Here are the methods I looked at.

1. readLine(): Looks like this method waits till it receives an end of
line, or the socket connection is closed.
2. read(char[] cbuf, int off, int len): Reads only specified number of
bytes, so it works as long as we specify at least as many bytes as
the client sent.

Thank you.


-C

javastudent wrote:
Hi,

I am implementing a server that reads for socket connections at a
port, and processes the socket on a separate thread. We do not control
the client implementation. Here is the scenario.

0. Client connects to the socket.
1. Client writes to the socket.
2. Server reads from the socket.
3. Client waits for the response on the socket
4. Server writes to the socket.
5. Socket is closed.

Incoming request on the socket is not guaranteed to be terminated with
any particular sequence of characters. How does the server know how
many bytes to read, and when to stop reading?

Here are couple of things I tried.

1. readln on a buffered reader on the server side would block till it
receives the "eol" character.
2. I have tried reading x number of characters at a time, and thought
if number of characters read are less than the attempted number of
characters to read (or the read method returns -1) then read all the
data sent by the client. This works if the attempted characters to
read initially, are greater than the number of characters sent by the
client, but not if it is the other way.

I have used BufferedReader, and the PrintWriter classes.

I appreciate your help in resolving this issue.

Thanks.

Jul 17 '05 #3
ne******@comcas t.net (javastudent) wrote in
news:d4******** *************** ***@posting.goo gle.com:
Chris Rohr <cj****@verizon .net> wrote in message
news:<OA******* **********@nwrd dc03.gnilink.ne t>...
When the client connects to the socket it only writes the one time
then waits, so you can use the BufferedReader to read all of the data
on the socket. Each communication from the client has a new socket,
so you don't have to maintain a connection and wonder how much data
is being sent. When the socket is flushed to the server, it will
append the eof to the stream. Hopes this helps at all.


Thanks for your reply. I really appreciate it.

What method in the BufferedReader class can I use to read all the data
on the socket? Here are the methods I looked at.

1. readLine(): Looks like this method waits till it receives an end of
line, or the socket connection is closed.
2. read(char[] cbuf, int off, int len): Reads only specified number of
bytes, so it works as long as we specify at least as many bytes as
the client sent.

Thank you.

This is what I did to request HTML data back:

Socket socket = new Socket(URL, HTTP_PORT);
BufferedWriter out = new BufferedWriter( new OutputStreamWri ter(socket
.getOutputStrea m()));

BufferedReader in = new BufferedReader( new InputStreamRead er(socket
.getInputStream ()));
out.write(outTa rgetrequest +" HTTP/1.0\n\n");
out.flush();

String line;
while ((line = in.readLine()) != null) {
System.out.prin tln(line);
}
out.close();
in.close();

--
Thanks in Advance...
IchBin
_______________ _______________ _______________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )
Jul 17 '05 #4
IchBin wrote:
while ((line = in.readLine()) != null) {
System.out.prin tln(line);

call me dumb, but isnt this Syste.out different from
your buffered output stream? e.g. should be just
out.println(lin e);

}
out.close();
in.close();

i am still trying to cope with all the magic hat tricks
java is unloading on newbie coders...
- nate

Jul 17 '05 #5
Yes, you are right.

This is not my final code. I had never done this before and wanted to test
the see the input right to the console. I now have other components using
this data, in my application. You have to start somewhere. Look up the
System.out.prin tln() as opposed to my instantiated BufferedWriter out. I am
witting out to a different place that what I Buffered in.on... LOL...

Sorry it's late for me.

--
Thanks in Advance...
IchBin
_______________ _______________ _______________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )
"Nate Smith" <gr*******@NET1 Plus.com> wrote in message
news:u4******** ************@ne t1plus.com...
IchBin wrote:
while ((line = in.readLine()) != null) {
System.out.prin tln(line);

call me dumb, but isnt this Syste.out different from
your buffered output stream? e.g. should be just
> out.println(lin e);

}
out.close();
in.close();

i am still trying to cope with all the magic hat tricks
java is unloading on newbie coders...
- nate

Jul 17 '05 #6
IchBin <we******@ptd.n et> wrote in message news:<Xn******* *************@2 04.186.200.105> ...
ne******@comcas t.net (javastudent) wrote in
news:d4******** *************** ***@posting.goo gle.com:
Chris Rohr <cj****@verizon .net> wrote in message
news:<OA******* **********@nwrd dc03.gnilink.ne t>...
When the client connects to the socket it only writes the one time
then waits, so you can use the BufferedReader to read all of the data
on the socket. Each communication from the client has a new socket,
so you don't have to maintain a connection and wonder how much data
is being sent. When the socket is flushed to the server, it will
append the eof to the stream. Hopes this helps at all.


Thanks for your reply. I really appreciate it.

What method in the BufferedReader class can I use to read all the data
on the socket? Here are the methods I looked at.

1. readLine(): Looks like this method waits till it receives an end of
line, or the socket connection is closed.
2. read(char[] cbuf, int off, int len): Reads only specified number of
bytes, so it works as long as we specify at least as many bytes as
the client sent.

Thank you.

This is what I did to request HTML data back:

Socket socket = new Socket(URL, HTTP_PORT);
BufferedWriter out = new BufferedWriter( new OutputStreamWri ter(socket
.getOutputStrea m()));

BufferedReader in = new BufferedReader( new InputStreamRead er(socket
.getInputStream ()));
out.write(outTa rgetrequest +" HTTP/1.0\n\n");
out.flush();

String line;
while ((line = in.readLine()) != null) {
System.out.prin tln(line);
}
out.close();
in.close();

You are relying on your clients terminating the string with "\n",
hence your readLine works. My clients wouldn't be ending the string
with "\n". However, I have just found out that we have a gate way that
terminates the string with '\004', so how do I use BufferedReader to
read until I find a termination character?

Thanks.
Jul 17 '05 #7
ne******@comcas t.net (javastudent) wrote in
news:d4******** *************** ***@posting.goo gle.com:
IchBin <we******@ptd.n et> wrote in message
news:<Xn******* *************@2 04.186.200.105> ...
ne******@comcas t.net (javastudent) wrote in
news:d4******** *************** ***@posting.goo gle.com:
> Chris Rohr <cj****@verizon .net> wrote in message
> news:<OA******* **********@nwrd dc03.gnilink.ne t>...
You are relying on your clients terminating the string with "\n",
hence your readLine works. My clients wouldn't be ending the string
with "\n". However, I have just found out that we have a gate way that
terminates the string with '\004', so how do I use BufferedReader to
read until I find a termination character?

Thanks.


Right off the top, not sure. Look at the InputStreamRead er,
OutputStreamWri ter for adding, changing or reconizing a termiation or
custom termination char, via (Java 2 Platform, Standard Edition, v 1.5.0
Beta 2 API Specification), Online or download it. Maybe even socket?

out.write(outTa rgetrequest +" terination char? )

Another good place to post this question is:comp.lang.ja va.developer or
comp.lang.java. programmer and maybe comp.lang.java. help. Don't cross post
and maybe Roedy Green or Andrew Thompson could help you out.

Sorry for now...
--
Thanks in Advance...
IchBin
_______________ _______________ _______________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )
Jul 17 '05 #8
Look at this documentation, I happen to bump into.. It's in the "Big Index"
at Sun. This page has most all common java information! Not as detailed
specifc as the java API specs. You should aways look here first.. anyway

Go here (http://java.sun.com/docs/books/tutor...ybigindex.html)

This link will take you to the instructions...
(http://java.sun.com/docs/books/tutor...ngWriting.html
)
Other good ones just in case... and should be your bible's, so to speak..
(just downlod the lastest SDK document zip.)

JavaTM 2 SDK, Standard Edition Documentation
JavaTM 2 Platform, Standard Edition, v 1.5.0 Beta 2 API Specification
Java Language Specification
--
Hope this helps.. I think it will
IchBin
_______________ _______________ _______________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )
"IchBin" <we******@ptd.n et> wrote in message
news:Xn******** ************@20 4.186.200.105.. .
ne******@comcas t.net (javastudent) wrote in
news:d4******** *************** ***@posting.goo gle.com:
IchBin <we******@ptd.n et> wrote in message
news:<Xn******* *************@2 04.186.200.105> ...
ne******@comcas t.net (javastudent) wrote in
news:d4******** *************** ***@posting.goo gle.com:

> Chris Rohr <cj****@verizon .net> wrote in message
> news:<OA******* **********@nwrd dc03.gnilink.ne t>...

You are relying on your clients terminating the string with "\n",
hence your readLine works. My clients wouldn't be ending the string
with "\n". However, I have just found out that we have a gate way that
terminates the string with '\004', so how do I use BufferedReader to
read until I find a termination character?

Thanks.


Right off the top, not sure. Look at the InputStreamRead er,
OutputStreamWri ter for adding, changing or reconizing a termiation or
custom termination char, via (Java 2 Platform, Standard Edition, v 1.5.0
Beta 2 API Specification), Online or download it. Maybe even socket?

out.write(outTa rgetrequest +" terination char? )

Another good place to post this question is:comp.lang.ja va.developer or
comp.lang.java. programmer and maybe comp.lang.java. help. Don't cross post
and maybe Roedy Green or Andrew Thompson could help you out.

Sorry for now...
--
Thanks in Advance...
IchBin
_______________ _______________ _______________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )

Jul 17 '05 #9
"IchBin" <we***********@ pdt.net> wrote in message news:<YG******* *************@p td.net>...
Look at this documentation, I happen to bump into.. It's in the "Big Index"
at Sun. This page has most all common java information! Not as detailed
specifc as the java API specs. You should aways look here first.. anyway

Go here (http://java.sun.com/docs/books/tutor...ybigindex.html)

This link will take you to the instructions...
(http://java.sun.com/docs/books/tutor...ngWriting.html
)
Other good ones just in case... and should be your bible's, so to speak..
(just downlod the lastest SDK document zip.)

JavaTM 2 SDK, Standard Edition Documentation
JavaTM 2 Platform, Standard Edition, v 1.5.0 Beta 2 API Specification
Java Language Specification
--
Hope this helps.. I think it will
IchBin

Yes, it did. Thanks for taking time.

Regards. _______________ _______________ _______________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )
"IchBin" <we******@ptd.n et> wrote in message
news:Xn******** ************@20 4.186.200.105.. .
ne******@comcas t.net (javastudent) wrote in
news:d4******** *************** ***@posting.goo gle.com:
IchBin <we******@ptd.n et> wrote in message
news:<Xn******* *************@2 04.186.200.105> ...
> ne******@comcas t.net (javastudent) wrote in
> news:d4******** *************** ***@posting.goo gle.com:
>
> > Chris Rohr <cj****@verizon .net> wrote in message
> > news:<OA******* **********@nwrd dc03.gnilink.ne t>... You are relying on your clients terminating the string with "\n",
hence your readLine works. My clients wouldn't be ending the string
with "\n". However, I have just found out that we have a gate way that
terminates the string with '\004', so how do I use BufferedReader to
read until I find a termination character?

Thanks.


Right off the top, not sure. Look at the InputStreamRead er,
OutputStreamWri ter for adding, changing or reconizing a termiation or
custom termination char, via (Java 2 Platform, Standard Edition, v 1.5.0
Beta 2 API Specification), Online or download it. Maybe even socket?

out.write(outTa rgetrequest +" terination char? )

Another good place to post this question is:comp.lang.ja va.developer or
comp.lang.java. programmer and maybe comp.lang.java. help. Don't cross post
and maybe Roedy Green or Andrew Thompson could help you out.

Sorry for now...
--
Thanks in Advance...
IchBin
_______________ _______________ _______________

'Black holes are where God divided by zero.'
-Steven Wright, comedian (1955- )

Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2518
by: hendershot | last post by:
Using SQL Server 2000 SP3a, I run the following in 2 query analizer windows on the Northwind database, the second one always gets the deadlock Msg 1205: Window 1: declare @cnt int select @cnt = 5 while @cnt > 0 begin
7
9223
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into a table (c) re-queries another table using a subquery which references the inserted table (correlated or not)
1
2359
by: debian mojo | last post by:
Hello faculties, i'm encountering a strange a deadlock problem on my remote server which is used to give application demo to the client. It has happened that on one of the databases a deadlock situation is taking place. What is the most detailed way to detect such the cause of such a deadlock to the innermost level of detail, like what statements, stored procedures and locks are causing the deadlock to occur.
8
9550
by: Javauser | last post by:
Hi there we are getting the following db2 error on executeBatch() method that inserts n rows on a table (where n is between 50 and 200). SQL0911N The current transaction has been rolled back because of a deadlock or timeout. Reason code "2". SQLSTATE=40001 errorCode : 911
8
2315
by: mk | last post by:
You probably suspect the answer, typically its 'yes' deadlock can occur in any multithreaded application. Even ones that employ static members. Commonly it occurs when more than one thread tries to lock a resource that another thread has already locked. A workaround is to use the Monitor object's TryEnter method, and pass a timeout value, if timeout occurs the method returns false. This pattern will save you no end of pain and...
13
2547
by: Jonathan Amsterdam | last post by:
I think there's a slight design flaw in the Queue class that makes it hard to avoid nested monitor deadlock. The problem is that the mutex used by the Queue is not easy to change. You can then easily get yourself into the following situation (nested monitor deadlock): Say we have a class that contains a Queue and some other things. The class's internals are protected by a mutex M. Initially the Queue is empty. The only access to the...
0
2301
by: Hemant Shah | last post by:
Folks, I have been getting SQLCODE -911 while reading data from a table. When I look at the db2diag.log file I see following: 2006-10-10-14.33.13.384726-240 I127058C470 LEVEL: Warning PID : 233904 TID : 1 PROC : db2agent (CFG) 0 INSTANCE: db2prod NODE : 000 DB : CFG APPHDL : 0-1415 APPID: *LOCAL.db2prod.061010183323 FUNCTION: DB2 UDB, access plan manager,...
2
4244
by: Dragan | last post by:
Hi, We're working in VS 2005, Team edition, if it makes any difference at all (should be up-to-date and all that, but could not guarantee it is 100%). We've implemented a simple generic wrapper parser under C++/CLI. It's a basic project created under Visual C++/CLR - Class Library - and it's pretty much what it is, just one /clr compiled class, fully CLR, no C++ native types or processing, nothing, just managed all the way. The reason...
1
1849
by: amoldiego | last post by:
Hi, I am updating same row of the Same Table in 2 diff session, i.e, for deadlock situation In 1st session declare a number(5) := 123; begin update ba_bank_mast set COD_BANK=a; end;
0
8968
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8787
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9334
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.