sockets - Unable to fix Error 10038 WSAENOTSOCK -


i'm trying learn berkeley socket programming, stuck here: have 10038 error both of server , client program, according msdn means: "an operation attempted on not socket" socket "socket". here server/client code:

my client :

#include <winsock.h> #include <stdio.h> #include <string.h>  #define buffsize 32  void diewitherror(char *errormessage);  int main(int argc, char* argv[]){      int sock, byteslen;     struct sockaddr_in servaddr;     unsigned short servport =13;     char * servip;     char buff[buffsize+1];     wsadata wsadata;      if(argc == 2){         servip = argv[1];     }      if(wsastartup(makeword(2,0), &wsadata) != 0){         puts("error wsa");     }      if(sock=socket(af_inet, sock_stream, 0) < 0){         puts("error socket");     }      memset(&servaddr, 0, sizeof(servaddr));     servaddr.sin_family   = af_inet;     servaddr.sin_port     = htons(servport);     servaddr.sin_addr.s_addr = inet_addr(&servip);      if(connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)<0)){         diewitherror("connect error");     }      while(byteslen=read(sock, buff, buffsize)){         buff[byteslen]= '\n';     }      printf("heure : %s",buff);      closesocket(sock);     wsacleanup();      return 0; }  void diewitherror(char *errormessage) {     fprintf(stderr,"%s: %d\n",     errormessage, wsagetlasterror());     exit(1); } 

and sever:

#include <winsock.h> #include <stdio.h> #include <string.h> #include <time.h> #include <stdlib.h>   #define maxline 32  void diewitherror(char *errormessage);  int main(){      int sock, sockc;     struct sockaddr_in servaddr;     unsigned short servport =13;     time_t ticks;     wsadata wsadata;     char buff[maxline];     char * servip;      if(wsastartup(makeword(2,0), &wsadata) != 0){         puts("error wsa");     }      if(sock=socket(af_inet, sock_stream, ipproto_tcp) < 0){         puts("error socket");     }     servip = "10.1.1.3";     memset(&servaddr, 0, sizeof(servaddr));     servaddr.sin_family   = af_inet;     servaddr.sin_port     = htons(servport);     servaddr.sin_addr.s_addr = htonl(inaddr_any);      if(bind(sock,(struct sockaddr *)&servaddr, sizeof(servaddr) < 0)){         diewitherror("bind)()");     }      if(listen(sock,maxqueue) < 0){         diewitherror("listen ()");     }      while(1){          sockc = accept(sock, (struct sockaddr *)null, null);         ticks = time(null);         snprintf(buff,sizeof(buff),"%s",ctime(&ticks));          write(buff, sizeof(buff), maxline);     }      return 0; }  void diewitherror(char *errormessage) {     fprintf(stderr,"%s: %d\n",         errormessage, wsagetlasterror());     exit(1); } 

each start have 10038 on connect() function of client , 10038 on bind() function on server.

thanks help.

unlike other platforms, windows not use file descriptors represent sockets. uses actual kernel objects. other platforms use int represent open file descriptors, , represent sockets. windows uses socket type instead, typedef uint_ptr (unsigned int in 32bit, unsigned __int64 in 64bit) can accommodate object handles. socket() function on windows returns invalid_socket (which (socket)(~0)) on error, while other platforms return (int)-1 instead. need make sure account not slice/truncate legitimate socket handles on windows.

when client calling inet_addr(), passing char** char* expected. compiler should have reported error on that.

as sockets not represented file descriptors on windows, cannot use read() , write() functions windows sockets. must use recv()/wsarecv() , send()/wsasend() functions instead. , need handle case functions can return socket_error (-1) on error.

try more instead:

client :

#include <winsock.h> #include <stdio.h> #include <string.h>  #define buffsize 32  void diewitherrorcode(char *errormessage, int errcode); void diewitherror(char *errormessage, int *errcode = null);  int main(int argc, char* argv[]) {     socket sock;     int ret;     struct sockaddr_in servaddr;     unsigned short servport = 13;     char *servip;     char buff[buffsize];     wsadata wsadata;      if (argc != 2) {         diewitherrorcode("error argc", wsaeinval);     }     servip = argv[1];      ret = wsastartup(makeword(2,0), &wsadata);     if (ret != 0) {         diewitherrorcode("error wsastartup", ret);     }      sock = socket(af_inet, sock_stream, ipproto_tcp);     if (sock == invalid_socket) {         diewitherror("error socket");     }      memset(&servaddr, 0, sizeof(servaddr));     servaddr.sin_family = af_inet;     servaddr.sin_port = htons(servport);     servaddr.sin_addr.s_addr = inet_addr(servip);     if (servaddr.sin_addr.s_addr == inet_none) {         diewitherrorcode("error inet_addr", wsaeinval);     }      if (connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) == socket_error) {         diewitherror("error connect");     }      while(1) {         ret = recv(sock, buff, buffsize, 0);         if (ret == socket_error) {             diewitherror("error recv");         }          if (ret == 0) {             fprintf(stderr, "disconnected\n");             break;         }          printf("heure : %.*s\n", ret, buff);     }      closesocket(sock);     wsacleanup();      return 0; }  void diewitherrorcode(char *errormessage, int errcode) {     fprintf(stderr, "%s: %d\n", errormessage, errcode);     exit(1); }  void diewitherror(char *errormessage) {     diewitherrorcode(errormessage, wsagetlasterror()); } 

server:

#include <winsock.h> #include <stdio.h> #include <string.h> #include <time.h> #include <stdlib.h>  #define maxline 32  void diewitherrorcode(char *errormessage, int errcode); void diewitherror(char *errormessage);  int main() {     socket sock, sockc;     struct sockaddr_in servaddr;     unsigned short servport = 13;     time_t ticks;     wsadata wsadata;     char buff[maxline];     int ret, buflen;      ret = wsastartup(makeword(2,0), &wsadata);     if (ret != 0) {         diewitherrorcode("error wsa", ret);     }      sock = socket(af_inet, sock_stream, ipproto_tcp);     if (sock == invalid_socket) {         diewitherror("error socket");     }      memset(&servaddr, 0, sizeof(servaddr));     servaddr.sin_family = af_inet;     servaddr.sin_port = htons(servport);     servaddr.sin_addr.s_addr = htonl(inaddr_any);      if (bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) == socket_error) {         diewitherror("error bind");     }      if (listen(sock, maxqueue) == socket_error) {         diewitherror("error listen");     }      while(1) {         sockc = accept(sock, null, null);         if (sockc == invalid_socket) {             diewitherror("error accept");         }          ticks = time(null);         buflen = snprintf(buff, sizeof(buff), "%s", ctime(&ticks));         if (send(sockc, buff, buflen, 0) == socket_error) {             fprintf(stderr, "error send: %d\n", wsagetlasterror());         }          closesocket(sockc);     }      closesocket(sock);     wsacleanup();      return 0; }  void diewitherrorcode(char *errormessage, int errcode) {     fprintf(stderr,"%s: %d\n", errormessage, errcode);     exit(1); }  void diewitherror(char *errormessage) {     diewitherrorcode(char *errormessage, wsagetlasterror()); } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -