Код:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
#define OK 0
#define FAIL 1
#define CONNECT_LOST1 2
#define CONNECT_LOST2 3
#define MSG_EMPTY 4
#define SOCKET_FAIL 5
#define MAX_LEN 100
#define PORT 110
#define _HOST "94.100.177.6"
using namespace std;
int SEND(SOCKET, char*);
int RECV(SOCKET, char*);
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "Russian");
int ans = -5;
char GET_MSG[1024];
WSAData WSData;
WSAStartup(0x101,&WSData);
SOCKET S = socket(AF_INET,SOCK_STREAM,0);
sockaddr_in SA;
SA.sin_family=AF_INET;
SA.sin_port=htons(PORT);
SA.sin_addr.S_un.S_addr=inet_addr(_HOST);
connect(S,(sockaddr*)&SA,sizeof(SA));
char SEND_MSG[1024];
string quit = "QUIT";
while(1){
cout << "->";
strcpy(SEND_MSG,"");
strcpy(GET_MSG, "");
scanf("%s", SEND_MSG);
ans = SEND(S, SEND_MSG);
switch(ans){
case FAIL: cout << "Server connection failed\n"; break;
case MSG_EMPTY: cout << "Message is empty\n"; break;
case CONNECT_LOST1: cout << "Server get empty message...\n"; break;
case CONNECT_LOST2: cout << "Server connection lost...\n"; break;
case OK:
printf(GET_MSG);
printf("\n");
break;
}
strcpy(SEND_MSG,"");
strcpy(GET_MSG, "");
scanf("%s", SEND_MSG);
ans1 = RECV(S, GET_MSG);
switch(ans){
case FAIL: cout << "Server connection failed\n"; break;
case MSG_EMPTY: cout << "Message is empty\n"; break;
case CONNECT_LOST1: cout << "Server send empty message...\n"; break;
case CONNECT_LOST2: cout << "Server connection lost...\n"; break;
case OK:
printf(GET_MSG);
printf("\n");
break;
}
}
closesocket(S);
WSACleanup();
cin.get();
system("pause");
return 0;
}
int SEND(SOCKET S,char* msg) {
if(S == INVALID_SOCKET) return SOCKET_FAIL;
if(S == NULL) return FAIL;
if(strlen(msg) == 0) return MSG_EMPTY;
int is_send;
if( (is_send = send(S, msg, sizeof(msg),MSG_DONTROUTE)) < 0)
return CONNECT_LOST2;
return OK;
}
int RECV(SOCKET S, char* msg) {
if(S == INVALID_SOCKET) return SOCKET_FAIL;
if(S == NULL) return FAIL;
int read_bytes;
if( (read_bytes = recvfrom(S, msg, sizeof(msg), 0, NULL, NULL)) < 0)
return CONNECT_LOST2;
if(read_bytes == 0) return MSG_EMPTY;
msg[ read_bytes ] = '\0';
return OK;
}
сервер не принимает вторую команду, помогите исправить