ورود

View Full Version : سوال: اشتباه این برنامه ی client کجاست؟



zoz_zozm
شنبه 16 بهمن 1389, 00:51 صبح
وقتی برنامه رو اجرا می کنم ارور میده و میگه نمی تونه به سوکت متصل بشه.


#include <Winsock2.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib") //Winsock2 Library

int main(int argc,char **argv)

{

WSADATA wsaData;


if(argc<=2){
printf("::Error on The call Program::\n");
printf("%s RemoteIPAddress RemotePort",argv[0]);
exit(1);
}

struct sockaddr_in saInfo;
saInfo.sin_family = AF_INET;
saInfo.sin_addr.S_un.S_addr=inet_addr(argv[1]);
saInfo.sin_port = htons(atoi(argv[2]));

WORD wVersionRequested;
wVersionRequested=MAKEWORD(2,0);
int err=WSAStartup(wVersionRequested,&wsaData);
if(err != 0){
printf("\n::Error On init Socket::\n");
//system("pause");
exit(1);
}

SOCKET sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sock==INVALID_SOCKET){
printf("\n::Error On Create Socket::\n");
printf("Error Code:%d",WSAGetLastError());
WSACleanup();
exit(1);
}

int intErr;
intErr=connect(sock, (struct sockaddr *)&saInfo, sizeof(sockaddr_in));
if(intErr==SOCKET_ERROR){
printf("\n::Error On Connect to Socket::\n");
printf("Error Code:%d",WSAGetLastError());
WSACleanup();
exit(1);
}

char *buffer= "Salam\0";
intErr=send(sock,buffer,strlen(buffer),0);//return the number of caracter that sent successfully
if(intErr==SOCKET_ERROR){
printf("\n::Error On Send Data::\n");
printf("Error Code:%d",WSAGetLastError());
WSACleanup();
exit(1);
}

memset(buffer,'\0',strlen(buffer));
intErr=recv(sock,buffer,strlen(buffer),0);
if(intErr==SOCKET_ERROR){
printf("\n::Error On Recev Data:5:\n");
printf("Error Code:%d",WSAGetLastError());
exit(1);
}
printf("\nRecived Data:%s\n",buffer);
closesocket(sock);
WSACleanup();
system("pause");

}