|
-
Mar 22nd, 2005, 10:16 AM
#1
Thread Starter
New Member
Winsock Help!
Hi everyone, first i will explain my set up:
Winsock Program as Client
Connects to my embedded PIC program, which is C code with a tcp/ip stack.
I can get them to connect but it will only work once.
The second time i get the run time error stating that:
socket not bound, invalid address or listen is not invoked prior to accept.
I am guessing there is a problem with the socket still being open.
I am unsure whether it is from my V Basic code or the c code?
Here is my VB Code:
VB Code:
' initialize variables
Private Sub Form_Load()
txtRemoteIpAddress = "192.168.0.20"
txtRemotePort = "13"
txtSocketStatus = SocketStatus(tcpClient.State)
txtLocalPort = tcpClient.LocalPort
txtLocalIpAddress = tcpClient.LocalIP
txtLocalHostName = tcpClient.LocalHostName
End Sub
' operator wants to connect to the server
Private Sub cmdConnect_Click()
If (tcpClient.State = sckClosed) Then
' Invoke the Connect method to initiate a connection.
tcpClient.RemotePort = txtRemotePort
tcpClient.RemoteHost = txtRemoteIpAddress
tcpClient.Connect
txtRemoteHostName = tcpClient.RemoteHost
Else
MsgBox "ERROR: socket is not closed. Socket must be closed before opening a new connection"
End If
End Sub
Private Sub cmdDisconnect_Click()
'Disconnect from ClientProcedure
' operator wants to exit the program
tcpClient.Close
End
End Sub
' the server disconnected the connection
Private Sub tcpClient_Close()
tcpClient.Close
End Sub
' the server sent us data
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
tcpClient.GetData strData, vbString
txtOutput.Text = strData
End Sub
' winsock control encountered an error
Private Sub tcpClient_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Debug.Print "Server Error Number = " + Str(Number) + ", Description = " + Description
txtSocketStatus = Description
End Sub
' this timer simply updates the status of the socket control on the display
Private Sub Timer1_Timer()
txtSocketStatus = SocketStatus(tcpClient.State)
' IF the socket is stuck in the closing or error states THEN close the socket to reset it
If ((tcpClient.State = sckClosing) Or (tcpClient.State = sckError)) Then
tcpClient.Close
End If
End Sub
Maybe i should have the vb code as the server and my embedded c code as the client?
Last edited by stratty; Mar 23rd, 2005 at 12:15 PM.
-
Mar 22nd, 2005, 10:21 AM
#2
KING BODWAD XXI
Re: Winsock Help!
What line does the error occur on?
You are sure the winsock is set for TCP/IP and not UDP see its properties
-
Mar 22nd, 2005, 10:30 AM
#3
Thread Starter
New Member
Re: Winsock Help!
The error occurs in
And yes the property is set to TCP
-
Mar 22nd, 2005, 10:51 AM
#4
Re: Winsock Help!
On the PC check with:
"netstat -a"
Then you can see if a port is still open or not.
Success
-
Mar 22nd, 2005, 10:59 AM
#5
Re: Winsock Help!
 Originally Posted by stratty
I can get them to connect but it will only work once.
The second time i get the run time error stating that:
socket not bound, invalid address or listen is not invoked prior to accept
From this error message, I would guess that your problem is with the server side, not the client side. It sounds like the server is not resetting itself after all of the active sockets are closed. Can you post some of the C code?
-
Mar 22nd, 2005, 11:09 AM
#6
KING BODWAD XXI
Re: Winsock Help!
Try setting the client port to 0 before connecting. This allows windows to select a free port. There is also nothing wrong with doing a close before a connect it infact prevents errors that somehow slip though
-
Mar 22nd, 2005, 11:43 AM
#7
Thread Starter
New Member
Re: Winsock Help!
 Originally Posted by BodwadUK
Try setting the client port to 0 before connecting. This allows windows to select a free port.
Im not sure what u mean, because my server code is only sending data out if it gets a connection on port 13.
Anyway here is my C code :
Code:
if (locport==PORT)
{ // Recognised port?
rack = ((DWORD)concount << 16);
rack += 0xffff;
concount++;
tflags = TSYN+TACK; // ..send SYN ACK
}
else // Unrecognised port?
tflags = TRST+TACK; // ..send reset
}
else if (rflags & TFIN) // Received FIN?
rseq += rpdlen+1; // ..ack all incoming data + FIN
else if (rflags & TACK) // ACK received?
{
if (rpdlen) // ..adjust Tx ack for Rx data
rseq += rpdlen;
else // If no data, don't send ack
tflags = 0;
if (locport==PORT && (WORD)rack==0)
{ // Daytime request?
putstr(DAYMSG);
tflags = TFIN+TACK; // Ack & close connection
}
-
Mar 23rd, 2005, 02:45 AM
#8
KING BODWAD XXI
Re: Winsock Help!
Only on the client in VB.
Because the client doesnt need to be on a fixed local port (Its connecting out not in) you can have any port. It is therefore best to allow windows to allocate a free port than to try and take a fixed port that could be in use
To do this just before any connect event add
tcpClient.localport = 0
tcpClient.Connect
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|