473,396 Members | 1,703 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.

Error 10042 - A bad option or level was specified in a getsockopt or setsockopt

6
I’m trying to build an application in VB.NET that read GPRS communication protocols, between a GPS Tracker and Server, but when my socket tries to begin receiving the data that came from the tracker I get the error 10042.

Anyone knows what can make this to happen??? And how to fix it???

(I also wrote a client app to prove it with my server side app and it works good)

I'm posting the entery code so you can see it well.
Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Net
  3. Imports System.Net.Sockets
  4. Imports System.Text
  5. Imports System.Threading
  6. Imports Microsoft.VisualBasic
  7.  
  8. ' State object for reading client data asynchronously
  9.  
  10. Public Class StateObject
  11.     ' Client  socket.
  12.     Public workSocket As Socket = Nothing
  13.     ' Size of receive buffer.
  14.     Public Const BufferSize As Long = 1078624
  15.     ' Receive buffer.
  16.     Public buffer(BufferSize) As Byte
  17.     ' Received data string.
  18.     Public sb As New StringBuilder
  19. End Class 'StateObject
  20.  
  21.  
  22. Public Class AsynchronousSocketListener
  23.     ' Thread signal.
  24.     Public Shared allDone As New ManualResetEvent(False)
  25.  
  26.     ' This server waits for a connection and then uses  asychronous operations to
  27.     ' accept the connection, get data from the connected client, 
  28.     ' echo that data back to the connected client.
  29.     ' It then disconnects from the client and waits for another client. 
  30.     Public Shared Sub Main()
  31.         ' Data buffer for incoming data.
  32.         Dim bytes() As Byte = New [Byte](1023) {}
  33.  
  34.         ' Establish the local endpoint for the socket.
  35.         Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
  36.         Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
  37.         Dim localEndPoint As New IPEndPoint(ipAddress, 8000)
  38.  
  39.         ' Create a TCP/IP socket.
  40.         Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  41.  
  42.         ' Bind the socket to the local endpoint and listen for incoming connections.
  43.         listener.Bind(localEndPoint)
  44.         listener.Listen(100)
  45.  
  46.         While True
  47.             ' Set the event to nonsignaled state.
  48.             allDone.Reset()
  49.  
  50.             ' Start an asynchronous socket to listen for connections.
  51.             Console.WriteLine("Waiting for a connection...")
  52.             listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), listener)
  53.  
  54.             ' Wait until a connection is made and processed before continuing.
  55.             allDone.WaitOne()
  56.         End While
  57.     End Sub 'Main
  58.  
  59.  
  60.     Public Shared Sub AcceptCallback(ByVal ar As IAsyncResult)
  61.         ' Get the socket that handles the client request.
  62.         Dim listener As Socket = CType(ar.AsyncState, Socket)
  63.         ' End the operation.
  64.  
  65.         Dim handler As Socket = listener.EndAccept(ar)
  66.  
  67.         ' Create the state object for the async receive.
  68.         Dim state As New StateObject
  69.         state.workSocket = handler
  70.         handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
  71.         'handler.BeginConnect 
  72.     End Sub 'AcceptCallback
  73.  
  74.  
  75.     Public Shared Sub ReadCallback(ByVal ar As IAsyncResult)
  76.         Dim content As String = String.Empty
  77.  
  78.         ' Retrieve the state object and the handler socket
  79.         ' from the asynchronous state object.
  80.         Dim state As StateObject = CType(ar.AsyncState, StateObject)
  81.         Dim handler As Socket = state.workSocket
  82.  
  83.         ' Read data from the client socket. 
  84.         Dim bytesRead As Integer = handler.EndReceive(ar)
  85.  
  86.         If bytesRead > 0 Then
  87.             ' There  might be more data, so store the data received so far.
  88.             state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
  89.  
  90.             ' Check for end-of-file tag. If it is not there, read 
  91.             ' more data.
  92.             content = state.sb.ToString()
  93.             If content.IndexOf("\r\n") > -1 Then
  94.                 ' All the data has been read from the 
  95.                 ' client. Display it on the console.
  96.                 Console.WriteLine("Read {0} bytes from socket. " + vbLf + " Data : {1}", content.Length, content)
  97.                 ' Echo the data back to the client.
  98.                 Send(handler, content)
  99.             Else
  100.                 ' Not all data received. Get more.
  101.                 handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
  102.             End If
  103.         End If
  104.     End Sub 'ReadCallback
  105.  
  106.     Private Shared Sub Send(ByVal handler As Socket, ByVal data As String)
  107.         ' Convert the string data to byte data using ASCII encoding.
  108.         Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)
  109.  
  110.         ' Begin sending the data to the remote device.
  111.         handler.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf SendCallback), handler)
  112.     End Sub 'Send
  113.  
  114.  
  115.     Private Shared Sub SendCallback(ByVal ar As IAsyncResult)
  116.         ' Retrieve the socket from the state object.
  117.         Dim handler As Socket = CType(ar.AsyncState, Socket)
  118.  
  119.         ' Complete sending the data to the remote device.
  120.         Dim bytesSent As Integer = handler.EndSend(ar)
  121.         Console.WriteLine("Sent {0} bytes to client.", bytesSent)
  122.  
  123.         handler.Shutdown(SocketShutdown.Both)
  124.         handler.Close()
  125.         ' Signal the main thread to continue.
  126.         allDone.Set()
  127.     End Sub 'SendCallback
  128. End Class 'AsynchronousSocketListener
May 25 '10 #1
15 6617
Banfa
9,065 Expert Mod 8TB
Could you post the socket related calls that your code makes.

Particularly any calls to getsockopt or setsockopt if the code calls them directly.
May 27 '10 #2
Frinavale
9,735 Expert Mod 8TB
Ok, this is an interesting problem.

I don't see anything that immediately jumps out at me as wrong in your code.

Btw, Banfa the getsockopt and setsockopt are not part of the OP's code: Article on some old school stuff concerning Winsock.
May 27 '10 #3
Banfa
9,065 Expert Mod 8TB
No that is true but his error message suggests they are getting called somewhere and I imagine that underneath it all the .NET library still calls out to WinSock2

Is SocketType.Stream correct, on MSDN the examples use SocketType::Stream

FYI Goyem I am more of a networking expert than a .NET expert but luckily we have Frinny in here for the .NET stuff.
May 27 '10 #4
Goyem
6
Banfa, I'm using a SocketType.Stream because i'm using a two way tcp connection. I don't know if that is correct but in theory it is correct.

I talked with the people that sold me the tracker, and they told me that maybe is because of the protocol that the tracker use. That only make me anger. lol.

I don't know what else 2 do, any ideas on how to manage this protocols? If you need I can upload de gprs comunication protocol between gps tracker and server...
May 27 '10 #5
Banfa
9,065 Expert Mod 8TB
No socket type stream is the correct one to use I was questioning the syntax

SocketType.Stream

as opposed to

SocketType::Stream

Can you say at what point in the code the error is reported and if any other information is given with the error report?
May 27 '10 #6
Frinavale
9,735 Expert Mod 8TB
The syntax looks fine to me :)
This is VB.NET not C#/C++
I seriously can't see anything wrong with the code (it's not the way I would have implemented it but it looks correct). Maybe seeing the sepcs for the device might help
May 27 '10 #7
Banfa
9,065 Expert Mod 8TB
Erm you have you thread title wrong

A bad option or level was specified in a getsockopt or setsockopt is error 10042

Error 10024 is

//
// MessageId: WSAEMFILE
//
// MessageText:
//
// Too many open sockets.
//
#define WSAEMFILE 10024L
So which error do you actually have?
May 27 '10 #8
Goyem
6
the error is 10042, sorry about that.
The error is generatered in code line 70.
May 27 '10 #9
Banfa
9,065 Expert Mod 8TB
Well I can tell you what the problem is, apparently the socket has not actually finished connecting properly by the time you call BeginReceive. It appears this is a bug in the .NET framework that existed in 3.5 as recently as August 2009.

Because the connection is not properly established when you call BeginReceive an exception is raise, in your case 10042 but others can occur apparently.

I can not locate a fix for this particular problem, there is a kludge which is to put a 5 - 10 second thread delay just before the BeginReceive call.

I would recommend with something like sockets that you consider putting some exception handling into your code (does VB.net do exception handling?).

You can get direct access to the source of all this information by googling "10042 BeginReceive"
May 27 '10 #10
Frinavale
9,735 Expert Mod 8TB
Yup VB.NET has Try Catch blocks :)
Just no curly braces
May 28 '10 #11
Plater
7,872 Expert 4TB
I have actually hit the "too many open sockets" issue when performing a stress test on some hardware. When I did a "NETSTAT -a" at a command prompt, I had a lot of scrolling, because I had filled them up.
Are you doing an excessive amount of opening/closing of sockets?
May 28 '10 #12
Goyem
6
Plater, I make a mistake in the title, error code is 10042.

Banfa, I did a research in what you suggest and added a Thread.Sleep before the BeginAccept and i got the same response.

Frinavale, the device that I'm using is a TK310 GPS Tracker, using GPRS comunication protocols between the tracker and server. Maybe the problem is on how to used the protocols, but the truth is that I got no idea on how to deal with it, or if i'm doing the communication correctly
May 28 '10 #13
Banfa
9,065 Expert Mod 8TB
It might be using GPRS but as far as the server is concerned it should be just another tcp connection. All the GPRS stuff should be being handled by your network operator.

P.S. Correcting number in the title
May 28 '10 #14
Goyem
6
I think I pass through this problem, but know I have another issue.
Know I'm getting error code 10054:an existing connection was forcibly closed by the remote host.

P.S. googling it
May 28 '10 #15
Goyem
6
Problem solve, the carrier that I was using, damage the data, so change carrier and problem solved...
Jun 9 '10 #16

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

Similar topics

1
by: nicki | last post by:
hi we have a link server....and the user credentials are not being trasfered to link server.....any thoughts?? we get the following error message Server message 18542 level 16 state 1 --...
3
by: mrwoopey | last post by:
Hi, I am using the example "Authenticate against the Active Directory by Using Forms Authentication and Visual Basic .NET": http://support.microsoft.com/default.aspx?scid=KB;EN-US;326340 ...
3
by: Starbuck | last post by:
Hi The following generates an error when Option Strict is On Can anytell tell me how to get round this please. Private Sub optWithTone_CheckedChanged(ByVal eventSender As System.Object, ByVal...
3
by: Dean Slindee | last post by:
Not sure how to fix this syntax error when Option Strict On is enforced. Can anyone help? Dim ctl As Control Dim sa() As String For Each ctl In pnlParameter.Controls sa =...
2
by: hfk0 | last post by:
Hello, I'm a newbie here and was wondering anyone could help me with this. I have a simple ASP.NET 2 web application running perfectly fine with IIS and SQLServerExpress installed locally on...
0
by: dmlinliverpool | last post by:
I am running VS.net 2005 Express and Sql Server 2005 Express. The DB and VS are both on the same PC. I cannot connect to a database from within VS. In Database Explorer I click Connect To...
0
by: Levon Haroityoinyan | last post by:
Hello all. I just want to use gcc followed options in my build -feliminate-unused-debug-symbols -felimi- nate-unused-debug-types gcc leads cc1plus: error: unrecognized option I don't...
3
by: Sally1053 | last post by:
I have created a procedure which start by fecthing data from DB-X and put in into the temporary memory. what im trying to do now is to take data from temporary memory insert/update DB-Y. But now...
1
by: Omendra | last post by:
Hi, I am using SQL Server 2005 and i am using Stored Procedure something like :- I am getting error:- Msg 170, Level 15, State 1, Line 18: Incorrect syntax near 'MAX'. DECLARE @jid AS...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...

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.