so, I have two programs, one for the client and one for the server. I am testing my programs between two computers, and if I have computer one be the client it works, but if computer two is the client it can't connect. Now, my internet is a bit glitchy, so is this an actual problem, or is it just my internet?
here's the client:
#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;
}
and here's the server:
#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;
}
to operate, have the server running first, and run the client. type in the ip of the server, then type stuff.
to find your ip for sure, go to command prompt, and type "ipconfig"