Are you sure the port is open on the server?
Printable View
Are you sure the port is open on the server?
Yes 100% sure
Sorry, there was a mistake in the code I presented.
Change this line
and you can add these right after as a checkVB Code:
clientService.sin_port = htons(4500)VB Code:
If clientService.sin_port = INVALID_SOCKET Then MsgBox "Invalid Port" Exit Sub End If If clientService.sin_addr = INADDR_NONE Then MsgBox "INVALID SOCKET" Exit Sub End If
Still says "Cannot Connect to Server" when server is online.
OK,
Here are two projects: one is a simple server built with Winsock control, the other is the client built with API.
Run the server, then the client, push the connect button on the client and it connects to the server. At least for me it did.
Ok works now.
Thanks alot moeur!
I appreciate all the help you have given me very much.
Now onto send ;)
And to use it:VB Code:
Public Function sendmsg(ByVal s As Long, ByRef buf As Variant, ByVal buflen As Long, ByVal flags As Long) As Long Dim out As Long out = send(s, buf, buflen, flags) sendmsg = out End Function
And on the Server side I have it in DataArrival:VB Code:
Dim msg As String msg = "testing" sendmsg ConnectSocket, msg, Len(msg), 0
Now, when I send the message via the sendmsg function - the server gets the data but it's all weird looking like this: "ôz { ZÂf¤ó"VB Code:
Dim sData As String tcpServer.GetData sData Open "C:\msgtext.txt" For Output As #1 Print #1, sData Close #1
I'm most likely doing it wrong like usual :rolleyes:
Any suggestions?
Don't use Variants or as Any.
VB Code:
Public Declare Function sendString _ Lib "ws2_32.dll" Alias "send" (ByVal s As Long, _ ByVal buf As String, _ ByVal buflen As Long, _ ByVal flags As Long) As Long
VB Code:
Dim bytesSent As Long Dim sendbuf As String sendbuf = Text1 bytesSent = sendString(ConnectSocket, sendbuf, Len(sendbuf), 0) MsgBox "Bytes Sent: " & bytesSent
Ok that works. Thanks!
Now, one more problem left. Which is.. how would i publically declare this:
Incase you don't understand what I mean I'll show you how it's done in C++:VB Code:
MySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
I need to do that in VB in global declarations at top of code, ya know? like this:Code:SOCKET MySocket = NULL;
So again all in all I want to doVB Code:
Dim MySocket As SOCKET ' globally declared Private Sub DummyFunc() ' this does nothing Im just showing this as example End Sub
SOCKET MySocket = NULL;
^ that in VB but I don't know how it's done.
Reason for this is because when I try to send messages using "send" it's not sending and working because it doesn't know the socket to send it through and in C++ SOCKT MySocket = NULL; declares it as the Socket (I think).
Like to send messages in C++:
Like in the bold above .. notice how "sock" is declared as "SOCKET" and in vb it's "Long" .. why C++ it's declared, in my opinion, Properly as a socket so the "_sendmsg" function knows what socket to send it through.... and in vb it's just declared as a Long?. Anyway... I want to somehow (globally?) declare the Socket so I can send messages to the server without having to always connect and all that stuff more than once, if it's possible.Code:int _sendmsg(SOCKET sock, char* buf, int len)
{
char* buffer = new char[len];
int msg = send(sock,buffer,len,0);
delete buffer;
return msg;
}
And in VB:
See notice in C++ it's "SOCKET sock" and vb it's "sock As Long"VB Code:
Function sendmsg(ByVal sock As Long, ByVal buf As String, ByVal buflen As Long) As Long Dim msg As Long msg = sendString(sock, buf, buflen,0) ' your sendString "send" function. can even be "send" yes sendmsg = msg End Function
I hope you understand what I mean :sick:
There is no difference between Socket in C++ and Long in VB, so just declare it as long.
I tried doing that but when i try to send data it wont send anything.
I talked to a friend of mine that's good with C++ and he said "SOCKET MySocket;" gets the handle of your socket you created called "MySocket". So I need to know how to get the handle of my socket and global declare it. Like this:
Now that's my next adventure :DVB Code:
Dim MySocket As FunctionToGetSocketHandle ' somehow
You declare MySocket globally as long.
You call the Socket function to set the value of MySocket to a valid socket.
Then you can use that value in any routine that wants a socket.
If you don't mind could you post an example?
I understand better seeing examples. Sorry :blush:
Maybe you should post what you are trying to do to make sure I understand.
I'm trying to do what you said :D
I put this at top of coding page for global declare?:Quote:
You declare MySocket globally as long
.................................VB Code:
Global ConnectSocket As Long
I done this:Quote:
You call the Socket function to set the value of MySocket to a valid socket
I understand that yes - now here's my send function:VB Code:
Public Function IsServerOnline() ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) If connect(ConnectSocket, service, lenB(service)) = SOCKET_ERROR Then Msgbox "failed to connect" End If IsServerOnline = 1 End Function
And to use it:VB Code:
Public Function mysend(ByVal s As Long, ByVal buf As String, ByVal buflen As Long) As Long Dim out As Long out = sendString(s, buf, buflen, 0) mysend = out End Function
See how it says "mysend ConnectSocket" ? well I don't understand how the mysend function knows what socket "ConnectSocket" is because the "ConnectSocket = socket(...,...,...)" is in the "IsServerOnline" function. You get what I mean? So I was told "SOCKET MySocket;" in C++ "SOCKET" gets the Handle to "MySocket" so you can use it anywhere in any function you want. I don't see how "Dim or Global ConnectSocket As Long" will get the handle of the socket since it's "Long" and C++ is "SOCKET". I know you said C++ SOCKET is same as Long in vb but it's just ... confusing and doesn't work for me. You understand what I'm trying to do? :sick:VB Code:
Private Sub Command1_Click() Dim msg As String msg = "testing" mysend ConnectSocket, msg, Len(msg) End Sub
The only thing that gets the handle to the socket is the socket functionThis is true in both C++ and VB. So you need to call this function before calling the SendString function.VB Code:
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
So to just call it I put:
in every function I want to send data?VB Code:
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
No,
if you declare ConnectSocket as public, then it will always hold the handle to the socket so you only have to call socket once and connect once
I tried doing this but it does't send anything :sick:VB Code:
Global or Dim ConnectSocket As Long ' globally declare it Private Sub Command1_Click() Dim msg As String msg = "testing" mysend ConnectSocket, msg, Len(msg) End Sub Public Function mysend(ByVal s As Long, ByVal buf As String, ByVal buflen As Long) As Long Dim out As Long out = sendString(s, buf, buflen, 0) mysend = out End Function
where do you run all that other code we talked about?
The WSAStartup stuff is in Sub Main() so it gets initialized at startup. And the socket an connect is in another function.
Here's an example of it:
^ But when I try to send the message it doesn't send anything. It doesn't work :ehh:VB Code:
Global ConnectSocket As Long Sub Main() ' ive even tried puting this WSAStartup stuff with the socket function Dim iResult As Long Dim mywsaData As WSAData iResult = WSAStartup(&H202, mywsaData) If iResult <> NO_ERROR Then MsgBox "Error at WSAStartup()" End If End Sub Public Function IsServerOnline() ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) If ConnectSocket = SOCKET_ERROR Then MsgBox "Error at socket" IsServerOnline = 0 End If Dim service As sockaddr_in service.sin_family = AF_INET service.sin_addr = inet_addr("66.66.66.66")'server ip service.sin_port = htons(4500) If connect(ConnectSocket, server, LenB(server)) = SOCKET_ERROR Then MsgBox "Failed to connect" IsServerOnline = 0 End If IsServerOnline = 1 End Function Public Function mysend(ByVal s As Long, ByVal buf As String, ByVal buflen As Long) As Long Dim out As Long out = sendString(s, buf, buflen, 0) mysend = out End Function Private Sub Command1_Click() ' just example Dim msg as String msg = "testing" mysend ConnectSocket, msg, Len(msg) End Sub
OK,
Give me a little time, I'm kind of busy right now, but I'll take a look when I get a chance. In the mean time, you could play around with your code to see under what conditions you could get it to send data like the demo program did.
Ok no problem. I'll be here ;)
Ok i got it sending data to the server working. But now the problem is receiving and displaying data in the client that's sent from the server.
I tried:
But it's not working :sick:VB Code:
Dim msg As String If myrecv(ConnectSocket, msg, Len(msg), 0) = SOCKET_ERROR Then MsgBox "error getting data" Else MsgBox msg End If
..............................
My myrecv function:
I have the "recvString" API declared the same as "sendString" you gave me.VB Code:
Public Function myrecv(ByVal s as Long, ByVal buf As String, ByVal buflen As Long, ByVal flags As Long) As Long Dim out As Long out = recvString(s, buf, buflen, 0) myrecv = out End Function
OK, try this
VB Code:
Public Declare Function recvString _ Lib "ws2_32.dll" Alias "recv" (ByVal s As Long, _ ByVal buf As String, _ ByVal buflen As Long, _ ByVal flags As Long) As LongVB Code:
Dim bytesSent As Long Dim bytesRecv As Long Dim recvbuf As String Dim myError As Long recvbuf = String(32, 0) bytesRecv = recvString(ConnectSocket, recvbuf, Len(recvbuf), 0) If bytesRecv <= 0 Then myError = WSAGetLastError If bytesRecv = 0 Or myError = WSAECONNRESET Then MsgBox "Connection Closed." Exit Sub Else MsgBox "Receive Error: " & Hex(myError) WSACleanup Exit Sub End If End If MsgBox "Message Received: " & "(" & CStr(bytesRecv) & ") " & recvbuf
Works. Thanks alot for all the help moeur.. I appreciate it.
You can download the soft calles Api-Guide from http://www.allapi.net/ which has good examples for all the api ,
Try the api (inet_addr) for your problem