468,771 Members | 1,925 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

Call For Help

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
Nov 17 '05 #1
0 816

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Greg Chapman | last post: by
4 posts views Thread by ruca | last post: by
4 posts views Thread by abcd | last post: by
7 posts views Thread by archana | last post: by
11 posts views Thread by yangsuli | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.