PDA

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



NIMA_1981
یک شنبه 13 فروردین 1391, 13:12 عصر
سلام دوستان میشه بگید چرا این 2 تا کد کار نمیکنه

کد سمت کلاینت

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define DEFAULT_IP "127.0.0.1"
#define DEFAULT_PORT "4323"
#pragma comment(lib, "Ws2_32.lib")
//define main function
int main(int argc, char **argv){
WSADATA wsaData;
SOCKET intSocket;
struct sockaddr_in recSin;
char *pchrBuffer;
int intErr;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,0), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
//Create a Socket
recSin.sin_addr.S_un.S_addr=inet_addr(DEFAULT_IP);
recSin.sin_family=AF_INET;
recSin.sin_port=htons(atoi(DEFAULT_PORT));
intSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(intSocket== INVALID_SOCKET){
printf("\nError on Create Socket\n");
WSACleanup();
}
intErr=connect(intSocket,&recSin,sizeof(recSin));
if(intErr==INVALID_SOCKET){
printf("\nError on Connect to Socket\n");
printf("failed: %d\n", WSAGetLastError());
WSACleanup();
exit(1);
}

// Send And Recive Data
pchrBuffer="naeim\0";
intErr=send(intSocket,pchrBuffer,strlen(pchrBuffer ),0);
if (intErr == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
WSACleanup();
exit(1);
}

printf("Bytes Sent: %d\n", intErr);

memset(pchrBuffer,'\0',strlen(pchrBuffer));
intErr=recv(intSocket,pchrBuffer,strlen(pchrBuffer ),0);
if(intErr==SOCKET_ERROR){
printf("\nError on recive data\n");
printf("Error Code:%d",WSAGetLastError());
exit(1);
}
printf("Bytes recive: %d\n", intErr);
printf("\nRecive Data:%s\n",pchrBuffer);
//disconect Socket
closesocket(intSocket);
WSACleanup();
}



کد سمت سرور

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define DEFAULT_IP "127.0.0.1"
#define DEFAULT_PORT "4323"
#pragma comment(lib, "Ws2_32.lib")
//define main function
int main(int argc, char **argv){
WSADATA wsaData;
WORD wVersionRequested;
SOCKET intSocket;
int intRSocket;
struct sockaddr_in recServer,recClient;
int intErr,intLen;
char *pchrBuffer;
wVersionRequested=MAKEWORD(2,0);
if(WSAStartup(wVersionRequested,&wsaData)){
printf("\nError on init Socket\n");
exit(1);
}
recServer.sin_addr.S_un.S_addr=INADDR_ANY;
recServer.sin_family=AF_INET;
recServer.sin_port=htons(atoi(DEFAULT_PORT));
intSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(intSocket==INVALID_SOCKET){
printf("\nError on create Socket\n");
WSACleanup();
exit(1);
}
intErr=bind(intSocket,&recServer,sizeof(recServer));

if(intErr==SOCKET_ERROR){
printf("\nError on the call bind Function\n");
WSACleanup();
exit(1);
}
intErr=listen(intSocket,SOMAXCONN);
if(intSocket==INVALID_SOCKET){
printf("\nError on the call listen functon\n");
WSACleanup();
exit(1);
}
intLen=sizeof(recClient);
intRSocket=accept(intSocket,&recClient,&intLen);
if(intRSocket==INVALID_SOCKET){
printf("\nError on the Accept Connection\n");
printf("Error Code:%d",WSAGetLastError());
WSACleanup();
exit(1);
}
pchrBuffer="salam\0";
intErr=send(intRSocket,pchrBuffer,strlen(pchrBuffe r),0);
if(intErr==SOCKET_ERROR){
printf("\nError on Send Data\n");
printf("Error Code:%d",WSAGetLastError());
WSACleanup();
exit(1);
}
memset(pchrBuffer,'\0',strlen(pchrBuffer));
intErr=recv(intSocket,pchrBuffer,strlen(pchrBuffer ),0);
if(intErr==SOCKET_ERROR){
printf("\nError on Send Data\n");
printf("Error Code:%d",WSAGetLastError());
exit(1);
}
printf("\nRecived Data:%s\n",pchrBuffer);
closesocket(intSocket);
WSACleanup();
getch();
return 0;
}



قسمت ارسال اطلاعات درست کار میکنه اما برای دریافت این خطا رو میده 10057 که این میشه


Socket is not connected. A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using sendto) no address was supplied. Any other type of operation might also return this error—for example, setsockopt setting SO_KEEPALIVE if the connection has been reset.

one hacker alone
دوشنبه 14 فروردین 1391, 11:38 صبح
دوستاني كه در اين زمينه آشنايي دارن ممنون ميشم كمك كنن

V0RTEX
دوشنبه 14 فروردین 1391, 19:24 عصر
کد زیاده و debug سخت شما http://www.codeproject.com/Articles/20947/Hello-world-application-for-socket-programming-in رو نگاه کنید شاید مشکل حل شد