I dunno, but I am almost certain that this code should work. I have checked it about a hundred times against some other source codes i have found on the internet. Shouldnt this be able to recieve and send messages to a client continuously??? Because right now it only can send messages.
Code:
#include <winsock2.h>
#include <iostream>
#include <stdio.h>
#include <process.h>
int startupServerForListening(unsigned short port);
void shutdownServer(int socket);
DWORD WINAPI Recieve(int nBytes2, int clientSocket, char* inMessage)
{
while (nBytes2 != 1){
nBytes2 = recv(clientSocket, inMessage, sizeof(inMessage), 0);
if (nBytes2 == SOCKET_ERROR) {
printf("Recv Failed!\n");
system("pause");
return 0;
}
printf("Message Received : nbytes2\n", inMessage);
}
}
int main() {
printf("Welcome to Sandips Online Messenger. . . !\n");
// the socket my server will use for listening
int serverSocket;
// startup our server utilities with my handy function
serverSocket = startupServerForListening(7654);
// check for errors
if (serverSocket == -1) {
printf("Network Startup Failed!\nProgram Terminating\n");
system("pause");
return 0;
}
// accept a client
int clientSocket;
clientSocket = accept(serverSocket, 0, 0);
// check for errors
if (clientSocket == SOCKET_ERROR) {
printf("Accept Failed!\n");
}
int nBytes;
#define MESSAGE_SIZE BUFSIZ
char inMessage[MESSAGE_SIZE];
char outMessage[MESSAGE_SIZE];
int nBytes2;
HANDLE hThrds[5];
DWORD aThreadID;
int i,j;
DWORD ThreadID;
hThrds[i] = (HANDLE) _beginthreadex(0,0,
(unsigned (_stdcall *)(void *)) Recieve,
(LPVOID) i,0, (unsigned *) &aThreadID);
while(outMessage != "/end")
{
printf("Message Sent : ", outMessage);
std::cin.getline (outMessage, MESSAGE_SIZE);
nBytes = send(clientSocket, outMessage, sizeof(outMessage), 0);
// check for errors
if (nBytes == SOCKET_ERROR) {
printf("Send Failed!\n");
system("pause");
return 0;
}
else {
}
}
closesocket(clientSocket);
shutdownServer(serverSocket);
system("PAUSE");
return 0;
}
// -----------------------------------------------------------------------------------
// startupServerForListening() - a function to startup winsock, and open a socket for listening
int startupServerForListening(unsigned short port) {
// the winsock data structure
WSAData wsaData;
// startup winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) == SOCKET_ERROR) {
printf("Could Not Start Up Winsock!\n");
return -1;
}
// create my socket
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
// make sure nothing bad happened
if (mySocket == SOCKET_ERROR) {
printf("Error Opening Socket!\n");
return -1;
}
// the address structure
struct sockaddr_in server;
// fill the address structure with appropriate data
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = INADDR_ANY;
// and now bind my socket
if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
printf("Bind Failed!\n");
closesocket(mySocket);
return -1;
}
// mark my socket for listening
if (listen(mySocket, 5) == SOCKET_ERROR) {
printf("Listen Failed!\n");
closesocket(mySocket);
return -1;
}
printf("Server Started\n");
printf("Waiting for client . . . \n");
//_endthread();
return mySocket;
}
// -----------------------------------------------------------------------------------
// shutdownServer() - a function to shutdown a socket and clean up winsock
void shutdownServer(int socket) {
// close our socket
closesocket(socket);
// shut down winsock
WSACleanup();
printf("Server Shutdown\n");
}