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.The re 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",ar gv[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 wVersionRequest ed = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
//
// Initialize WinSock and check the version
//
nRet = WSAStartup(wVer sionRequested, &wsaData);
if (wsaData.wVersi on != wVersionRequest ed)
{
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",ar gv[0]);
exit(1);
}
// The gethostbyname() function is used when we have the DNS name
of our server
h = gethostbyname(a rgv[1]);
if(h==NULL) {
printf("%s: unknown host '%s'\n",argv[0],argv[1]);
exit(1);
}
//
// Fill in the address structure
//
servAddr.sin_fa mily = h->h_addrtype;
memcpy((char *) &servAddr.sin_a ddr.s_addr, h->h_addr_list[0],
h->h_length);
servAddr.sin_po rt = htons(SERVER_PO RT);
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
perror("cannot open socket ");
exit(1);
}
/* bind any port number */
localAddr.sin_f amily = AF_INET;
localAddr.sin_a ddr.s_addr = htonl(INADDR_AN Y);
localAddr.sin_p ort = htons(0);
rc = bind(sd, (struct sockaddr *) &localAddr,
sizeof(localAdd r));
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