For some reason, I can't seem to connect. I'm using Winsock.
This is my client program:
and this is the server program:Code:#include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma lib "ws2_32.lib" int main() { char recvstuff[100]; char sendstuff[100]; char ip[100]; printf("type friend's ip address: "); fgets(ip, 25, stdin); // intialise winsock WSADATA wsaData; if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { printf("error one"); getchar(); exit(1); } // getaddrinfo call struct addrinfo *result = NULL, *ptr = NULL, hints; ZeroMemory(&hints, sizeof (hints)); hints.ai_family=AF_UNSPEC; hints.ai_socktype=SOCK_STREAM; hints.ai_protocol=IPPROTO_TCP; if (getaddrinfo(ip, "7772", &hints, &result) != 0) { printf("error two"); getchar(); exit(1); } int sock; // make a socket ptr=result; sock=socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); if (sock == -1) { printf("error three"); getchar(); exit(1); } // connect to a server if (connect(sock, ptr->ai_addr, (int)ptr->ai_addrlen) == -1) { printf("error four"); getchar(); exit(1); } printf("connected\n\n\n\n"); freeaddrinfo(result); // main send and recieve loop while (strcmp(fgets(sendstuff, 102, stdin), "exit\n") != 0) { while (strcspn(sendstuff, "\n") == 102) { fgets(sendstuff, 102, stdin); if (send(sock, sendstuff, sizeof (sendstuff), 0) == -1) { printf("error five"); getchar(); exit(1); } } if (send(sock, sendstuff, sizeof (sendstuff), 0) == -1) { printf("error six"); getchar(); exit(1); } if (recv(sock, recvstuff, 100, 0) == -1) { printf("error seven"); getchar(); exit(1); } printf("%s\n", recvstuff); } // shut down winsock and end program closesocket(sock); WSACleanup(); return 0; }
run the server before the client, and when you run the client, type the ip of the server then just type like an IMing serviceCode:#include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma lib "ws2_32.lib" int main() { char recvstuff[100]; char sendstuff[100]="hi daniel!"; // intialise winsock WSADATA wsaData; if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { printf("WSAStartup failed\n\n"); exit(1); } // getaddrinfo call struct addrinfo *result=NULL, hints; ZeroMemory(&hints, sizeof (hints)); hints.ai_family=AF_INET; hints.ai_socktype=SOCK_STREAM; hints.ai_protocol=IPPROTO_TCP; hints.ai_flags=AI_PASSIVE; if (getaddrinfo(NULL, "7772", &hints, &result) != 0) { printf("getaddrinfo failed\n\n"); exit(1); } // make a socket int sock=socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (sock == -1) { printf("socket failed\n\n"); exit(1); } // bind the socket if (bind(sock, result->ai_addr, result->ai_addrlen) == -1) { printf("bind failed\n\n"); exit(1); } freeaddrinfo(result); // listen on the socket if (listen(sock, SOMAXCONN) == -1) { printf("listen failed\n\n"); exit(1); } // wait for connection int mainsock; mainsock=accept(sock, NULL, NULL); if (mainsock == -1) { printf("accept failed"); exit(1); } printf("connected\n\n\n\n"); closesocket(sock); while (recv(mainsock, recvstuff, 100, 0) != -1) { if (send(mainsock, sendstuff, sizeof (sendstuff), 0) == -1) { printf("send failed\n\n"); exit(1); } printf("%s", recvstuff); } printf("they exited"); getchar(); closesocket(mainsock); WSACleanup(); return 0; }



LinkBack URL
About LinkBacks


