error with simple sockets progrma

Started by eagle3, May 12, 2009, 10:32:18 AM

Previous topic - Next topic

eagle3

hi there

i just work on a socket program and it's buggy

just halted at the first execution functions

the code file is attatched

plz tell me the error


JohnF

I've corrected a few things but it does not connect for some reason.


#include <windows.h>
#include <winsock.h>
#include <stdio.h>

#pragma comment(lib,"ws2_32.lib")

int main(void)
{
WSADATA wData;
struct sockaddr_in addr;
SOCKET sock;
char buf[256];
int ret = 0;

if (WSAStartup(0x202, &wData) != 0)
{
puts("Error INitializing WSA");
return GetLastError();
}


sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
{
puts("Error Creating socket");
return (-1);
}

addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
addr.sin_addr.s_addr = (unsigned long)inet_addr("192.168.1.17");

ret = connect(sock, (SOCKADDR *) &addr, sizeof(addr));
if (ret == -1)
{
puts("Error connecting");
return (-1);
}

send(sock, "Hello WOrld!", 15, 0);
recv(sock, buf, 256, 0);

closesocket(sock);
WSACleanup();

return (0);
}


John


eagle3

ell john

thnx for u to help me

but it still do error when exectuing

cann't even run



JohnF

Quote from: eagle3 on May 14, 2009, 07:33:28 AM
ell john

thnx for u to help me

but it still do error when exectuing

cann't even run


It runs here so I can't say why it doesn't for you. It prints ""Error connecting"

John

eagle3


JohnF

We can try to sort the problem.

Exactly what errors are you getting?

John