Reputation: 1
Hello i try to open socket with arguments (you know argv and argc) but for some reason the program shows that error WSAStratup @ 8 and some things like that. I think the problem is related to the income of the arguments.
I would be happy if you can see and help me understand why this is happening thanks.
My code -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
int main(int argc,char *argv[])
{
WSADATA info;
int err, s, cResult;
char ch[10] = "";
char cl[10] = "";
strcpy(ch, argv[3]);
err = WSAStartup(MAKEWORD(2, 0), &info);
if (err != 0)
{
printf("WSAtartup faild\n");
return(1);
}
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf("Error creating = %d\n", WSAGetLastError());
}
else
{
printf("socket function succeeded\n");
}
SOCKADDR_IN clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(argv[1]);
clientService.sin_port = htons(atoi(argv[2]));
cResult = connect(s, (struct sockaddr *) &clientService, sizeof (clientService));
if (cResult == SOCKET_ERROR)
{
printf("connect failed error: %ld\n", WSAGetLastError());
}
send(s, ch, 10, 0);
recv(s, cl, 10, 0);
puts(ch);
puts(cl);
if (strcmp(ch, cl) == 0)
{
printf(" Same\n");
}
else
{
printf("Not same\n");
}
cResult = closesocket(s);
if (cResult == SOCKET_ERROR)
{
printf("closesocket function failed error %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
system("PAUSE");
}
Upvotes: 0
Views: 230
Reputation: 79
It's not clear from your question what is failing, but to correctly initialize Winsock 2 supported functions, you want to call
WSAStartup(WINSOCK_VERSION, &wsadata);
Where WINSOCK_VERSION is defined in Winsock2.h as:
#define WINSOCK_VERSION MAKEWORD(2,2)
You'll also want to link with ws2_32.lib to use Winsock 2 functions (Winsock 1.1 is really old - that version is exported from wsock32.lib).
Upvotes: 1
Reputation: 1969
I've ran your program in my Visual studio and it runs fine. You should take into account several things: when calling your program you should specified an IP, port number and some test message.
myprogram 127.0.0.1 1024 "test message"
Also, you should check every socket command, like send()
and recv()
if they execute or give an error. See the modified program. And finally you should link your program with the wsock32.lib
library. Go to Project properties and add this library to the Additional dependencies in the Linker page, depends on your language, I have in Spanish and it says "Dependencias adicionales" maybe your Visual Studio says something different.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
int main(int argc,char *argv[])
{
WSADATA info;
int err, s, cResult;
char ch[10] = "";
char cl[10] = "";
// Check that the appropiate arguments has been given
if (argc < 4) return -1;
// Copy up the size of ch
strncpy(ch, argv[3], sizeof(ch));
err = WSAStartup(MAKEWORD(2, 0), &info);
if (err != 0)
{
printf("WSAtartup failed: %ld\n", WSAGetLastError());
system("PAUSE");
return -1;
}
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf("Error creating = %ld\n", WSAGetLastError());
system("PAUSE");
return -1;
}
else
{
printf("socket function succeeded\n");
}
SOCKADDR_IN clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(argv[1]);
clientService.sin_port = htons(atoi(argv[2]));
cResult = connect(s, (struct sockaddr *) &clientService, sizeof (clientService));
if (cResult == SOCKET_ERROR)
{
printf("connect failed error: %ld\n", WSAGetLastError());
system("PAUSE");
return -1;
}
if (send(s, ch, 10, 0) > 0)
{
puts(ch);
if (recv(s, cl, 10, 0) > 0)
{
puts(cl);
if (strcmp(ch, cl) == 0)
{
printf(" Same\n");
}
else
{
printf("Not same\n");
}
}
else
{
printf("error in recv() %ld\n", WSAGetLastError());
}
}
else
{
printf("error in send() %ld\n", WSAGetLastError());
}
cResult = closesocket(s);
if (cResult == SOCKET_ERROR)
{
printf("closesocket function failed error %ld\n", WSAGetLastError());
}
WSACleanup();
system("pause");
return 0;
}
Upvotes: 0