Hello cornholio,
I played with your code and I have questions about it, but rather than ask you, I thought it might be easier if I posted a simple client/server example, then you could see if maybe your code could be corrected. (You probably don't need this server, but I give it anyway.)
Here goes...the Client simply repeatedly sets up/ breaks down a connection.
The simple Client:
Code:
// I compile/link with gcc (let me know if you want the vc compile/link commands)
//gcc myclient.c -lws2_32 -o myclient.exe
#include <windows.h>
#include <stdio.h>
#include <winsock.h>
#define DEST_IP_ADDR "127.0.0.1"
#define PORT (u_short) 13
int main()
{
#define MAXBUFLEN 256
WSADATA Data;
SOCKET destSocket;
SOCKADDR_IN destSockAddr;
unsigned long destAddr;
int status;
int numsnt;
char sendText[MAXBUFLEN] = "Some message";
while(1) {
status = WSAStartup(MAKEWORD(1, 1), &Data);
if (status != 0)
{ printf("WSAStartup unsuccessful\n"); }
destSocket = socket(AF_INET, SOCK_STREAM, 0);
if (destSocket == INVALID_SOCKET) {
printf("socket unsuccessful\n");
status = WSACleanup();
if (status == SOCKET_ERROR)
{ printf("WSACleanup unsuccessful\n"); }
return(1);
}
destAddr = inet_addr(DEST_IP_ADDR);
memcpy(&destSockAddr.sin_addr, &destAddr, sizeof(destAddr));
destSockAddr.sin_port = htons(PORT);
destSockAddr.sin_family = AF_INET;
printf("Trying to connect to IP Address: %s\n",DEST_IP_ADDR);
status = connect(destSocket, (LPSOCKADDR) &destSockAddr, sizeof(destSockAddr));
if (status == SOCKET_ERROR) {
printf("connect unsuccessful\n");
status = closesocket(destSocket);
if (status == SOCKET_ERROR)
{ printf("closesocket unsuccessful\n"); }
status = WSACleanup();
if (status == SOCKET_ERROR)
{ printf("WSACleanup unsuccessful\n"); }
return(1);
}
printf("Connected...\n");
//printf("Type text to send: ");
//gets(sendText);
numsnt = send(destSocket, sendText, strlen(sendText) + 1, 0);
if (numsnt != (int)strlen(sendText) + 1) {
printf("Connection terminated.\n");
status = closesocket(destSocket);
if (status == SOCKET_ERROR)
{ printf("closesocket unsuccessful\n"); }
status = WSACleanup();
if (status == SOCKET_ERROR)
{ printf("WSACleanup unsuccessful\n"); }
return(1);
}
status = closesocket(destSocket);
if (status == SOCKET_ERROR)
{ printf("closesocket unsuccessful\n"); }
status = WSACleanup();
if (status == SOCKET_ERROR)
{ printf("WSACleanup unsuccessful\n"); }
printf("Connection terminated.\n");
//Sleep(10000); // wait 10 seconds
} /* while */
}
The simple Server:
Code:
//gcc myserver.c -lws2_32 -o myserver.exe
#include <windows.h>
#include <stdio.h>
#include <winsock2.h>
#include <process.h>
#define NO_FLAGS_SET 0
#define PORT (u_short) 13
#define MAXBUFLEN 256
VOID talkToClient(VOID *cs) {
char buffer[MAXBUFLEN];
int status;
int numsnt;
int numrcv;
SOCKET clientSocket = (SOCKET)cs;
while(1) {
numrcv = recv(clientSocket, buffer, MAXBUFLEN, NO_FLAGS_SET);
if ((numrcv == 0) || (numrcv == SOCKET_ERROR)) {
printf("Connection terminated\n");
break;
}
//printf("n= %i",numrcv);
buffer[numrcv] = '\0';
printf("Received: %s\n",buffer);
//_strupr(buffer);
numsnt = send(clientSocket, buffer, strlen(buffer) + 1, NO_FLAGS_SET);
if (numsnt != (int)strlen(buffer) + 1) {
printf("Connection terminated.");
break;
}
} /* while */
status = shutdown(clientSocket, 2);
if (status == SOCKET_ERROR)
{ printf("shutdown unsuccessful"); }
status = closesocket(clientSocket);
if (status == SOCKET_ERROR)
{ printf("closesocket unsuccessful"); }
}
int main(VOID) {
WSADATA Data;
SOCKADDR_IN serverSockAddr;
SOCKADDR_IN clientSockAddr;
SOCKET serverSocket;
SOCKET clientSocket;
int addrLen=sizeof(SOCKADDR_IN);
int status;
DWORD threadID;
/* initialize the Windows Socket DLL */
status=WSAStartup(MAKEWORD(1, 1), &Data);
if (status != 0) {
printf("WSAStartup unsuccessful");
return(1);
}
memset(&serverSockAddr, 0, sizeof(serverSockAddr));
serverSockAddr.sin_port = htons(PORT);
serverSockAddr.sin_family = AF_INET;
serverSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET) {
printf("ERROR: socket unsuccessful");
status = WSACleanup();
if (status == SOCKET_ERROR)
{ printf("WSACleanup unsuccessful"); }
return(1);
}
status = bind(serverSocket, (LPSOCKADDR) &serverSockAddr, sizeof(serverSockAddr));
if (status == SOCKET_ERROR)
{ printf("bind unsuccessful"); }
status = listen(serverSocket, 1);
if (status == SOCKET_ERROR)
{ printf("listen unsuccessful"); }
while(1) {
clientSocket = accept(serverSocket, (LPSOCKADDR) &clientSockAddr, &addrLen);
if (clientSocket == INVALID_SOCKET) {
printf("Unable to accept connection");
return(1);
}
threadID = _beginthread(talkToClient, 0, (VOID *)clientSocket);
if (threadID == -1) {
printf("Unable to create thread");
status = closesocket(clientSocket);
if (status == SOCKET_ERROR)
{ printf("closesocket unsuccessful"); }
}
}
}