khalilov 0 Report post Posted November 11, 2011 Client /* client.c - code for example client program that uses TCP */#ifndef unix#define WIN32#include <windows.h>#include <winsock.h>#else#define closesocket close#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#endif#pragma comment(lib,"Ws2_32.lib")#include <stdio.h>#include <string.h>// link with Ws2_32.lib#pragma comment(lib,"Ws2_32.lib")#define PROTOPORT 5193 /* default protocol port number */extern int errno;//char localhost[] = "localhost"; /* default host name */char localhost[] = "127.0.0.1"; /* default host name *//*------------------------------------------------------------------------ * Program: client * * Purpose: allocate a socket, connect to a server, and print all output * * Syntax: client [ host [port] ] * * host - name of a computer on which server is executing * port - protocol port number server is using * * Note: Both arguments are optional. If no host name is specified, * the client uses "localhost"; if no protocol port is * specified, the client uses the default given by PROTOPORT. * *------------------------------------------------------------------------ */ char records[100][256]; int recordi=0; char message[100]; int messagei=0;void receivedata(void* sd){while(1) { int n,i; char buf[256]; n = recv(sd, buf, sizeof(buf), 0); while (n > 0) { flushall(); n = recv(sd, buf, sizeof(buf), 0); sprintf(records[recordi],"%s",buf); system("CLS"); for(i=0;i<=recordi;i++) printf("%s",records[i]); printf("Message: %s",message); recordi++; } puts(""); } /* Close the socket. */ closesocket(sd);}int main(int argc, char* argv[]){ int i; struct hostent *ptrh; /* pointer to a host table entry */ struct protoent *ptrp; /* pointer to a protocol table entry */ struct sockaddr_in sad; /* structure to hold an IP address */ int sd; /* socket descriptor */ int port; /* protocol port number */ char *host; /* pointer to host name */ int n; /* number of characters read */ char buf[256]; /* buffer for data from the server */#ifdef WIN32 WSADATA wsaData; WSAStartup(0x0101, &wsaData);#endif memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ /* Check command-line argument for protocol port and extract */ /* port number if one is specified. Otherwise, use the default */ /* port value given by constant PROTOPORT */ if (argc > 2) { /* if protocol port specified */ port = atoi(argv[2]); /* convert to binary */ } else { port = PROTOPORT; /* use default port number */ } if (port > 0) /* test for legal value */ sad.sin_port = htons((u_short)port); else { /* print error message and exit */ fprintf(stderr,"bad port number %s\n",argv[2]); getchar(); exit(1); } /* Check host argument and assign host name. */ if (argc > 1) { host = argv[1]; /* if host argument specified */ } else { host = localhost; } /* Convert host name to equivalent IP address and copy to sad. */ ptrh = gethostbyname(host); if ( ((char *)ptrh) == NULL ) { fprintf(stderr,"invalid host: %s\n", host); getchar(); exit(1); } memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length); /* Map TCP transport protocol name to protocol number. */ if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) { fprintf(stderr, "cannot map \"tcp\" to protocol number"); getchar(); exit(1); } /* Create a socket. */ sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto); if (sd < 0) { fprintf(stderr, "socket creation failed\n"); getchar(); exit(1); } /* Connect the socket to the specified server. */ if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) { fprintf(stderr,"connect failed\n"); getchar(); exit(1); } printf("Connected to %s at port %d\n",host,sd); _beginthread(receivedata, 0, (void*)sd); while(1) { message[messagei]=getch(); messagei++; message[messagei]='\0'; printf("%c",message[messagei-1]);} /* Repeatedly read data from socket and write to user's screen. */ /* Terminate the client program gracefully. */ } Server#include<winsock2.h>#include<winsock.h>#include <stdio.h>#include <windows.h> //Not necessary if using MFC#include <TChar.h> //..#include <assert.h>#include<conio.h>// link with Ws2_32.lib#pragma comment(lib,"Ws2_32.lib")#include<process.h>#include <ws2tcpip.h>#include <stdlib.h> #define PROTOPORT 5193 /* default protocol port number */#define QLEN 6 int visits = 0; /* counts client connections *//*------------------------------------------------------------------------ * Program: server * * Purpose: allocate a socket and then repeatedly execute the following: * (1) wait for the next connection from a client * (2) send a short message to the client * (3) close the connection * (4) go back to step (1) * * Syntax: server [ port ] * * port - protocol port number to use * * Note: The port argument is optional. If no port is specified, * the server uses the default given by PROTOPORT. * *------------------------------------------------------------------------ */ char message[100]; char buf[256]; int i; char c; char records[100][256]; int recordi=0; int messagei=0; void serverThread(void* sd){ while(1) { int n,i; char buf[256]; n = recv(sd, buf, sizeof(buf), 0); //while (n > 0) { flushall(); //n = recv(sd, buf, sizeof(buf), 0); sprintf(records[recordi],"%s",buf); system("CLS"); for(i=0;i<=recordi;i++) printf("%s",records[i]); printf("Message: %s",message); recordi++; //} puts(""); } // closesocket(sd);}int main(int argc, char* argv[]){ struct hostent *ptrh; /* pointer to a host table entry */ struct protoent *ptrp; /* pointer to a protocol table entry */ struct sockaddr_in sad; /* structure to hold server's address */ struct sockaddr_in cad; /* structure to hold client's address */ int sdmain,sd, sd2,sd3; /* socket descriptors */ int port; /* protocol port number */ int alen; /* length of address */ char buf[256]; /* buffer for string the server sends */#ifdef WIN32 WSADATA wsaData; WSAStartup(0x0101, &wsaData);#endif memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */ /* Check command-line argument for protocol port and extract */ /* port number if one is specified. Otherwise, use the default */ /* port value given by constant PROTOPORT */ if (argc > 1) { /* if argument specified */ port = atoi(argv[1]); /* convert argument to binary */ } else { port = PROTOPORT; /* use default port number */ } if (port > 0) /* test for illegal value */ sad.sin_port = htons((u_short)port); else { /* print error message and exit */ fprintf(stderr,"bad port number %s\n",argv[1]); exit(1); } /* Map TCP transport protocol name to protocol number */ if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) { fprintf(stderr, "cannot map \"tcp\" to protocol number"); exit(1); } /* Create a socket */ sdmain = socket(PF_INET, SOCK_STREAM, ptrp->p_proto); sd=sdmain; if (sd < 0) { fprintf(stderr, "socket creation failed\n"); exit(1); } /* Bind a local address to the socket */ if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) { fprintf(stderr,"bind failed\n"); exit(1); } /* Specify size of request queue */ if (listen(sd, QLEN) < 0) { fprintf(stderr,"listen failed\n"); exit(1); } /* Main server loop - accept and handle requests */ while (1) { alen = sizeof(cad); if ( (sd2=accept(sd, 0, 0)) < 0) { fprintf(stderr, "accept failed\n"); exit(1); } // create a thread printf("Connection established on socket %d \n",sd2);_beginthread(serverThread, 0, (void*)sd2); while(1){ gets(message); sprintf(buf,"Server: %s\n", message); //send(sd2,buf,sizeof(buf),0); for(i=strlen(buf);i<sizeof(buf);i++) buf[i]='\0'; send(sd2, buf, sizeof(buf), 0); } closesocket(sd2); printf("Connection terminated on %d\n Waiting for new connections",sd2); getch(); }} ErrorError 1 error C2664: 'recv' : cannot convert parameter 1 from 'void *' to 'SOCKET' c:\users\pc\documents\visual studio 2005\projects\a\a\server.cpp 43 I don't understand, the server coding is similar to the client coding yet the server gives an error but the client doesnt. It works perfectly.If you remove the threading and the serverThread from server only it will work.The declaration is similar on both server and client scripts so I don't get it 8/Note: This might not be the best way to make this script, I am working on a small P2P chat using C,C++ sockets and I am still in the basis. But I encountered this error. I really hate C errors, I've wasted like two hours on changing variables and googeling with no result.The compiling was done using Visual studio 2005 8.0.5 Share this post Link to post Share on other sites
yordan 10 Report post Posted November 11, 2011 are both client and server on the same hardware platform? Same C compiler? Looks like a syntax difference. Share this post Link to post Share on other sites
khalilov 0 Report post Posted November 11, 2011 are both client and server on the same hardware platform? Same C compiler? Looks like a syntax difference.yes both are on same computer, and when I remov the threading, or the recv function in particular from the server script it works without erros Share this post Link to post Share on other sites