当前位置: 技术问答>linux和unix
UNIX下有关通信的系统函数的调用,如何快速入门??跳楼大放送——————100分!!!
来源: 互联网 发布时间:2014-12-19
本文导语: 兄弟我最近才开始学习UNIX(SCO UNIX),手上的项目主要和底曾通信打交道。看里面有关通信的系统函数,很烦,看的俺头都大了。 请问有经验的前辈,如何迅速入门?或者哪里有比较好的资料下载?或者哪里有比较...
兄弟我最近才开始学习UNIX(SCO UNIX),手上的项目主要和底曾通信打交道。看里面有关通信的系统函数,很烦,看的俺头都大了。
请问有经验的前辈,如何迅速入门?或者哪里有比较好的资料下载?或者哪里有比较专业一点的论坛?或者谈谈你自己的学习经验!!!
真诚请教!!!
请问有经验的前辈,如何迅速入门?或者哪里有比较好的资料下载?或者哪里有比较专业一点的论坛?或者谈谈你自己的学习经验!!!
真诚请教!!!
|
#include
#include
#include
#include
#include
int TCPSEND(int fd,char *buf,int len)
{
int i=0;
int send_rc;
for(;i=10)
return(-1);
else
return(send_rc);
}
int TCPRECEIVE(int fd,char *buf,int len)
{
int i=0;
int receive_rc;
for(;i0&&len=10)
return(-1);
else
return(receive_rc);
}
main(argc,argv)
int argc;
char **argv;
{
int i=0;
int sockfd;
FILE *tempfile;
char tempbuf[1024];
struct sockaddr_in serv_addr;
struct servent *sp;
struct hostent *npstHostent;
char info[100];
int infolen;
int rc;
if(argc!=2)
{
fprintf(stderr,"usage : client ipaddressn");
exit(-1);
}
sp=getservbyname("ATC_SERVICE","tcp");
npstHostent=gethostbyname("ATC_SERVER");
for(i=0;ih_addr,&(serv_addr.sin_addr),sizeof(serv_addr.sin_addr));
serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
// serv_addr.sin_port=htons(20208);
serv_addr.sin_port=sp->s_port;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))=0)
break;
shutdown(sockfd,2);
close(sockfd);
}
if(i>=10)
exit(-1);
memset(info,0,sizeof(info));
if((rc==TCPRECEIVE(sockfd,info,-sizeof(info)))==-1){
fprintf(stderr,"TCPReceive key failed!!n");
shutdown(sockfd,2);
close(sockfd);
exit(0);
}
if((rc=TCPSEND(sockfd,info,strlen(info)))==-1){
fprintf(stderr,"TCPSend key failed!!n");
shutdown(sockfd,2);
close(sockfd);
exit(0);
}
memset(info,0,sizeof(info));
if((rc==TCPRECEIVE(sockfd,info,-sizeof(info)))==-1){
fprintf(stderr,"TCPReceive answer failed!!n");
shutdown(sockfd,2);
close(sockfd);
exit(0);
}
if(strcmp(info,"ok")!=0){
fprintf(stderr,"Server reject service!!n");
shutdown(sockfd,2);
close(sockfd);
exit(0);
}
memset(info,0,sizeof(info));
printf("enter account no:");
scanf("%s",info);
if((rc=TCPSEND(sockfd,info,strlen(info)))==-1){
fprintf(stderr,"TCPSend failed!!n");
shutdown(sockfd,2);
close(sockfd);
exit(0);
}
tempfile=fopen("result.txt","wt+");
memset(tempbuf,0,sizeof(tempbuf));
while((rc=TCPRECEIVE(sockfd,tempbuf,-sizeof(tempbuf)))!=-1)
{
fputs(tempbuf,tempfile);
printf(tempbuf);
if(strncmp(tempbuf,"eof",3)==0) break;
memset(tempbuf,0,sizeof(tempbuf));
}
fclose(tempfile);
shutdown(sockfd,2);
close(sockfd);
}
#include
#include
#include
#include
int TCPSEND(int fd,char *buf,int len)
{
int i=0;
int send_rc;
for(;i=10)
return(-1);
else
return(send_rc);
}
int TCPRECEIVE(int fd,char *buf,int len)
{
int i=0;
int receive_rc;
for(;i0&&len=10)
return(-1);
else
return(receive_rc);
}
main(argc,argv)
int argc;
char **argv;
{
int i=0;
int sockfd;
FILE *tempfile;
char tempbuf[1024];
struct sockaddr_in serv_addr;
struct servent *sp;
struct hostent *npstHostent;
char info[100];
int infolen;
int rc;
if(argc!=2)
{
fprintf(stderr,"usage : client ipaddressn");
exit(-1);
}
sp=getservbyname("ATC_SERVICE","tcp");
npstHostent=gethostbyname("ATC_SERVER");
for(i=0;ih_addr,&(serv_addr.sin_addr),sizeof(serv_addr.sin_addr));
serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
// serv_addr.sin_port=htons(20208);
serv_addr.sin_port=sp->s_port;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))=0)
break;
shutdown(sockfd,2);
close(sockfd);
}
if(i>=10)
exit(-1);
memset(info,0,sizeof(info));
if((rc==TCPRECEIVE(sockfd,info,-sizeof(info)))==-1){
fprintf(stderr,"TCPReceive key failed!!n");
shutdown(sockfd,2);
close(sockfd);
exit(0);
}
if((rc=TCPSEND(sockfd,info,strlen(info)))==-1){
fprintf(stderr,"TCPSend key failed!!n");
shutdown(sockfd,2);
close(sockfd);
exit(0);
}
memset(info,0,sizeof(info));
if((rc==TCPRECEIVE(sockfd,info,-sizeof(info)))==-1){
fprintf(stderr,"TCPReceive answer failed!!n");
shutdown(sockfd,2);
close(sockfd);
exit(0);
}
if(strcmp(info,"ok")!=0){
fprintf(stderr,"Server reject service!!n");
shutdown(sockfd,2);
close(sockfd);
exit(0);
}
memset(info,0,sizeof(info));
printf("enter account no:");
scanf("%s",info);
if((rc=TCPSEND(sockfd,info,strlen(info)))==-1){
fprintf(stderr,"TCPSend failed!!n");
shutdown(sockfd,2);
close(sockfd);
exit(0);
}
tempfile=fopen("result.txt","wt+");
memset(tempbuf,0,sizeof(tempbuf));
while((rc=TCPRECEIVE(sockfd,tempbuf,-sizeof(tempbuf)))!=-1)
{
fputs(tempbuf,tempfile);
printf(tempbuf);
if(strncmp(tempbuf,"eof",3)==0) break;
memset(tempbuf,0,sizeof(tempbuf));
}
fclose(tempfile);
shutdown(sockfd,2);
close(sockfd);
}
|
是不是socket啊,用于TCP/IP通讯的?
#include
#include
#include
#include
#include
#include
#include
#include
int TCPSEND(int fd,char *buf,int len)
{
int i=0;
int send_rc;
for(;i=10)
return(-1);
else
return(send_rc);
}
int TCPRECEIVE(int fd,char *buf,int len)
{
int i=0;
int receive_rc;
for(;i0&&len=10)
return(-1);
else
return(receive_rc);
}
void reaper(int sig)
{
int status;
// Wait for child process complete exit
// If any error, system don't hangup
while(wait3(&status,WNOHANG,(struct rusage *)0)>=0);
}
#define FreeNewSockfd() {shutdown(newsockfd,2);close(newsockfd);exit(0);}
main()
{
int newsockfd,sockfd;
int clilen;
struct sockaddr_in cli_addr,serv_addr;
char info[100];
char tempbuf[1024];
FILE *tempfile;
int infolen;
int rc;
struct servent *sp;
//
char AddressList[48][16];
int i,AddressNo;
char AddressId[11],FileName[13];
char cmd[100];
for(i=0;i
#include
#include
#include
#include
#include
#include
#include
#include
int TCPSEND(int fd,char *buf,int len)
{
int i=0;
int send_rc;
for(;i=10)
return(-1);
else
return(send_rc);
}
int TCPRECEIVE(int fd,char *buf,int len)
{
int i=0;
int receive_rc;
for(;i0&&len=10)
return(-1);
else
return(receive_rc);
}
void reaper(int sig)
{
int status;
// Wait for child process complete exit
// If any error, system don't hangup
while(wait3(&status,WNOHANG,(struct rusage *)0)>=0);
}
#define FreeNewSockfd() {shutdown(newsockfd,2);close(newsockfd);exit(0);}
main()
{
int newsockfd,sockfd;
int clilen;
struct sockaddr_in cli_addr,serv_addr;
char info[100];
char tempbuf[1024];
FILE *tempfile;
int infolen;
int rc;
struct servent *sp;
//
char AddressList[48][16];
int i,AddressNo;
char AddressId[11],FileName[13];
char cmd[100];
for(i=0;i
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。