用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧Visual C++

Winsock程序设计初步之<三> 源程序实例(2)

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 20:26:58

//A simplest web client

//Written by Shen zhiliang for learning Winsock & HTTP

//zhiliang@sina.com

//1998.7.29

#include "winsock.h"

#include "stdio.h"

#define BUFLEN 4096

void SocketError(char * call)

{

fprintf(stderr," WinSock Error# function: %s, error code:%ld",call,WSAGetLastError());

}

main(int argc,char ** argv)

{

int iRes,iPort,iTmp;

SOCKET s,rs;

SOCKADDR_IN sin,rsin;

WSADATA wsad;

WORD wVersionReq;

char recvBuf[BUFLEN];

if(argc<4)

{

fprintf(stderr,"Usage: sockserver ip port message");

return -1;

}

if(inet_addr(argv[1])==INADDR_NONE)

{

fprintf(stderr,"Error ip gaving");

return -1;

}

iPort=0;

iPort=atoi(argv[2]);

sin.sin_addr.s_addr=inet_addr(argv[1]);

sin.sin_family=PF_INET;

sin.sin_port=htons(iPort);

if(iPort<=0)

{

fprintf(stderr,"must specify a number for port");

return -1;

}

wVersionReq=MAKEWORD(1,1);

iRes=WSAStartup(wVersionReq,&wsad);

if(iRes!=0)

{

SocketError("WSAStartup()");

return -1;

}

s=socket(PF_INET,SOCK_STREAM,0);

if(s==INVALID_SOCKET)

{

SocketError("socket()");

return -1;

}

iTmp=sizeof(sin);

fprintf(stderr,"WinSock Client Start......");

if(connect(s,(LPSOCKADDR)&sin,iTmp)==SOCKET_ERROR)

{

SocketError("connect()");

closesocket(s);

WSACleanup();

return -1;

}

strcpy(recvBuf,argv[3]);

strcat(recvBuf,"");

iRes=send(s,recvBuf,strlen(recvBuf),0);

if(iRes==SOCKET_ERROR)

{

SocketError("send()");

closesocket(s);

WSACleanup();

return -1;

}

printf("Sent Data:------------------%s------------------",recvBuf);

FILE * fp=fopen("send.dat","a+b");

if(fp==NULL)

return -1;

iRes=recv(s,recvBuf,BUFLEN,0);

while(iRes!=SOCKET_ERROR&&iRes!=0)

{

printf("Received Data:------------------%s------------------",recvBuf);

fwrite(recvBuf,sizeof(char),iRes,fp);

iRes=recv(s,recvBuf,BUFLEN,0);

}

fclose(fp);

closesocket(s);

WSACleanup();

return 0;

}


Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:28,015.63000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号