This is my first time doing any sort of network programming
I have both a server and a client that can connect to each other, the client can send data to the server but not vice versa
server:
Code:
void Init_Network()
{
if (WSAStartup(MAKEWORD(2,2), &WSAData) == SOCKET_ERROR)
MessageBox(0, "ERROR", "ERROR", MB_OK);
SockFD = socket(PF_INET, SOCK_STREAM, 0);
if (SockFD == SOCKET_ERROR)
MessageBox(0, "ERROR", "ERROR", MB_OK);
memset(My_Addr.sin_zero, '\0', sizeof My_Addr.sin_zero);
My_Addr.sin_family = AF_INET;
My_Addr.sin_port = htons(Port_Number);
My_Addr.sin_addr.s_addr = INADDR_ANY;
if (bind(SockFD, (struct sockaddr *)&My_Addr, sizeof(struct sockaddr)) == SOCKET_ERROR)
MessageBox(0, "ERROR", "ERROR", MB_OK);
if (listen(SockFD, 5) == SOCKET_ERROR)
MessageBox(0, "ERROR", "ERROR", MB_OK);
}
Code:
//send
OutMessage[0] = 'c';
OutMessage[1] = MyPos.X;
OutMessage[2] = MyPos.Y;
OutMessage[3] = '\0';
nBytes = send(SockFD, OutMessage, 20, 0);
std::cout<<errno;
memset(OutMessage, ' ', 20);
client:
Code:
void Init_Network()
{
if (WSAStartup(MAKEWORD(2,2), &WSAData) == SOCKET_ERROR)
MessageBox(0, "ERROR", "ERROR", MB_OK);
SockFD = socket(PF_INET, SOCK_STREAM, 0);
if (SockFD == SOCKET_ERROR)
MessageBox(0, "ERROR", "ERROR", MB_OK);
Host_Entry = gethostbyname(IP);
if (Host_Entry == NULL)
MessageBox(0, "ERROR", "ERROR", MB_OK);
Server_Addr.sin_family = AF_INET;
Server_Addr.sin_port = htons(Port_Number);
Server_Addr.sin_addr.s_addr = *(unsigned long*)Host_Entry->h_addr;
}
Code:
//recv
EnemyPrevPos.X = EnemyPos.X;
EnemyPrevPos.Y = EnemyPos.Y;
error = recv(ServerFD, InMessage, 20, 0);
if (error == 0)
{
std::cout<<"Your opponent has left the game."<<std::endl;
return 0;
}
if (error == -1)
{
char temps[50];
SetConsoleCursorPosition(hOut, tempC);
perror(temps);
std::cout<<temps<<std::endl;
std::cout<<errno<<std::endl;
//return 0;
}
switch (InMessage[0])
{
case 'c':
EnemyPos.X = InMessage[1];
EnemyPos.Y = InMessage[2];
break;
default:
SetConsoleCursorPosition(hOut, tempC);
std::cout<<InMessage;
break;
}
memset(InMessage, ' ', sizeof InMessage);