I had developed the client program under Visual C++ 6.0. I use the
Windows socket programming. I compile and build my program using the
Win32 Console application.There is no errors and warnings. But after
I executing the program, I found that if(argc < 3) {
printf("usage: %s <server> <data1> <data2> ...
<dataN>\n",argv[0]);
exit(1);
}
the output is <server> <data1> <data2> ...
<dataN> and stop with executing the rest of the program.My work
is to send data to the server PC which is under the Linux
environment.
The following is my program:Thanks for helping.
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#define SERVER_PORT 1500
#define MAX_MSG 100
//int CreatFile(char *szServer, short nPort);
//*CreatFile();
int main (int argc, char *argv[]) {
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
//
// Initialize WinSock and check the version
//
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
return 0;
}
int sd, rc, i;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
if(argc < 3) {
printf("usage: %s <server> <data1> <data2> ...
<dataN>\n",argv[0]);
exit(1);
}
// The gethostbyname() function is used when we have the DNS name
of our server
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s: unknown host '%s'\n",argv[0],argv[1]);
exit(1);
}
//
// Fill in the address structure
//
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0],
h->h_length);
servAddr.sin_port = htons(SERVER_PORT);
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
perror("cannot open socket ");
exit(1);
}
/* bind any port number */
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &localAddr,
sizeof(localAddr));
if(rc<0) {
printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT);
perror("error ");
exit(1);
}
/* connect to server */
rc = connect(sd, (struct sockaddr *) &servAddr,
sizeof(servAddr));
if(rc<0) {
perror("cannot connect ");
exit(1);
}
for(i=2;i<argc;i++){
rc = send(sd, argv, strlen(argv[i]) + 1, 0);
if(rc<0) {
perror("cannot send data ");
closesocket(sd);
exit(1);
}
printf("%s: data%u sent (%s)\n",argv[0],i-1,argv[i]);
}
//CreatFile(argv[1], SERVER_PORT);
//*CreatFile();
// Release WinSock
//
WSACleanup();
return 0;
}
*---------------------------------*
Posted at: http://www.GroupSrv.com
Check: http://wwww.HotCodecs.com
*---------------------------------*
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com