423,095 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,095 IT Pros & Developers. It's quick & easy.

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

P: 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
Share this Question
Share on Google+
15 Replies


Banfa
Expert Mod 5K+
P: 8,916
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
Expert Mod 5K+
P: 9,731
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
Expert Mod 5K+
P: 8,916
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

P: 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
Expert Mod 5K+
P: 8,916
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
Expert Mod 5K+
P: 9,731
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
Expert Mod 5K+
P: 8,916
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

P: 6
the error is 10042, sorry about that.
The error is generatered in code line 70.
May 27 '10 #9

Banfa
Expert Mod 5K+
P: 8,916
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
Expert Mod 5K+
P: 9,731
Yup VB.NET has Try Catch blocks :)
Just no curly braces
May 28 '10 #11

Plater
Expert 5K+
P: 7,872
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

P: 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
Expert Mod 5K+
P: 8,916
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

P: 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

P: 6
Problem solve, the carrier that I was using, damage the data, so change carrier and problem solved...
Jun 9 '10 #16

Post your reply

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