469,323 Members | 1,667 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

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

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
  8. ' State object for reading client data asynchronously
  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
  22. Public Class AsynchronousSocketListener
  23.     ' Thread signal.
  24.     Public Shared allDone As New ManualResetEvent(False)
  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) {}
  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)
  39.         ' Create a TCP/IP socket.
  40.         Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  42.         ' Bind the socket to the local endpoint and listen for incoming connections.
  43.         listener.Bind(localEndPoint)
  44.         listener.Listen(100)
  46.         While True
  47.             ' Set the event to nonsignaled state.
  48.             allDone.Reset()
  50.             ' Start an asynchronous socket to listen for connections.
  51.             Console.WriteLine("Waiting for a connection...")
  52.             listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), listener)
  54.             ' Wait until a connection is made and processed before continuing.
  55.             allDone.WaitOne()
  56.         End While
  57.     End Sub 'Main
  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.
  65.         Dim handler As Socket = listener.EndAccept(ar)
  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
  75.     Public Shared Sub ReadCallback(ByVal ar As IAsyncResult)
  76.         Dim content As String = String.Empty
  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
  83.         ' Read data from the client socket. 
  84.         Dim bytesRead As Integer = handler.EndReceive(ar)
  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))
  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
  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)
  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
  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)
  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)
  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 6080
9,064 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
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
9,064 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
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
9,064 Expert Mod 8TB
No socket type stream is the correct one to use I was questioning the syntax


as opposed to


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
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
9,064 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
the error is 10042, sorry about that.
The error is generatered in code line 70.
May 27 '10 #9
9,064 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
9,735 Expert Mod 8TB
Yup VB.NET has Try Catch blocks :)
Just no curly braces
May 28 '10 #11
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
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
9,064 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
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
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.

Similar topics

3 posts views Thread by Starbuck | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.