博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一些服务器客户端的c例子
阅读量:6479 次
发布时间:2019-06-23

本文共 8358 字,大约阅读时间需要 27 分钟。

今天早上6点起床之后练习的一些c的网络编程的基础例子

client1

/*  Make the necessary includes and set up the variables.  */#include 
#include
#include
#include
#include
#include
int main(){ int sockfd; int len; struct sockaddr_un address; int result; char ch = 'A';/* Create a socket for the client. */ sockfd = socket(AF_UNIX, SOCK_STREAM, 0);/* Name the socket, as agreed with the server. */ address.sun_family = AF_UNIX; strcpy(address.sun_path, "server_socket"); len = sizeof(address);/* Now connect our socket to the server's socket. */ result = connect(sockfd, (struct sockaddr *)&address, len); if(result == -1) { perror("oops: client1"); exit(1); }/* We can now read/write via sockfd. */ write(sockfd, &ch, 1); read(sockfd, &ch, 1); printf("char from server = %c\n", ch); close(sockfd); exit(0);}

  server1.c

/*  Make the necessary includes and set up the variables.  */#include 
#include
#include
#include
#include
#include
int main(){ int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_un server_address; struct sockaddr_un client_address;/* Remove any old socket and create an unnamed socket for the server. */ unlink("server_socket"); server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);/* Name the socket. */ server_address.sun_family = AF_UNIX; strcpy(server_address.sun_path, "server_socket"); server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *)&server_address, server_len);/* Create a connection queue and wait for clients. */ listen(server_sockfd, 5); while(1) { char ch; printf("server waiting\n");/* Accept a connection. */ client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);/* We can now read/write to client on client_sockfd. */ read(client_sockfd, &ch, 1); ch++; write(client_sockfd, &ch, 1); close(client_sockfd); }}

  第二个服务器客户端的例子:

client

/*  Make the necessary includes and set up the variables.  */#include 
#include
#include
#include
#include
#include
#include
int main(){ int sockfd; int len; struct sockaddr_in address; int result; char ch = 'A';/* Create a socket for the client. */ sockfd = socket(AF_INET, SOCK_STREAM, 0);/* Name the socket, as agreed with the server. */ address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr("127.0.0.1"); address.sin_port = htons(9734); len = sizeof(address);/* Now connect our socket to the server's socket. */ result = connect(sockfd, (struct sockaddr *)&address, len); if(result == -1) { perror("oops: client3"); exit(1); }/* We can now read/write via sockfd. */ write(sockfd, &ch, 1); read(sockfd, &ch, 1); printf("char from server = %c\n", ch); close(sockfd); exit(0);}

  server

/*  Make the necessary includes and set up the variables.  */#include 
#include
#include
#include
#include
#include
#include
int main(){ int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address;/* Remove any old socket and create an unnamed socket for the server. */ server_sockfd = socket(AF_INET, SOCK_STREAM, 0);/* Name the socket. */ server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons(9734); server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *)&server_address, server_len);/* Create a connection queue and wait for clients. */ listen(server_sockfd, 5); while(1) { char ch; printf("server waiting\n");/* Accept a connection. */ client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);/* We can now read/write to client on client_sockfd. */ read(client_sockfd, &ch, 1); ch++; write(client_sockfd, &ch, 1); close(client_sockfd); }}

  

/*  As usual, make the appropriate includes and declare the variables.  */#include 
#include
#include
#include
#include
#include
int main(int argc, char *argv[]){ char *host, **names, **addrs; struct hostent *hostinfo;/* Set the host in question to the argument supplied with the getname call, or default to the user's machine. */ if(argc == 1) { char myname[256]; gethostname(myname, 255); host = myname; } else host = argv[1];/* Make the call to gethostbyname and report an error if no information is found. */ hostinfo = gethostbyname(host); if(!hostinfo) { fprintf(stderr, "cannot get info for host: %s\n", host); exit(1); }/* Display the hostname and any aliases it may have. */ printf("results for host %s:\n", host); printf("Name: %s\n", hostinfo -> h_name); printf("Aliases:"); names = hostinfo -> h_aliases; while(*names) { printf(" %s", *names); names++; } printf("\n");/* Warn and exit if the host in question isn't an IP host. */ if(hostinfo -> h_addrtype != AF_INET) { fprintf(stderr, "not an IP host!\n"); exit(1); }/* Otherwise, display the IP address(es). */ addrs = hostinfo -> h_addr_list; while(*addrs) { printf(" %s", inet_ntoa(*(struct in_addr *)*addrs)); addrs++; } printf("\n"); exit(0);}

  

/*  Start with the usual includes and declarations.  */#include 
#include
#include
#include
#include
#include
int main(int argc, char *argv[]){ char *host; int sockfd; int len, result; struct sockaddr_in address; struct hostent *hostinfo; struct servent *servinfo; char buffer[128]; if(argc == 1) host = "localhost"; else host = argv[1];/* Find the host address and report an error if none is found. */ hostinfo = gethostbyname(host); if(!hostinfo) { fprintf(stderr, "no host: %s\n", host); exit(1); }/* Check that the daytime service exists on the host. */ servinfo = getservbyname("daytime", "tcp"); if(!servinfo) { fprintf(stderr,"no daytime service\n"); exit(1); } printf("daytime port is %d\n", ntohs(servinfo -> s_port));/* Create a socket. */ sockfd = socket(AF_INET, SOCK_STREAM, 0);/* Construct the address for use with connect... */ address.sin_family = AF_INET; address.sin_port = servinfo -> s_port; address.sin_addr = *(struct in_addr *)*hostinfo -> h_addr_list; len = sizeof(address);/* ...then connect and get the information. */ result = connect(sockfd, (struct sockaddr *)&address, len); if(result == -1) { perror("oops: getdate"); exit(1); } result = read(sockfd, buffer, sizeof(buffer)); buffer[result] = '\0'; printf("read %d bytes: %s", result, buffer); close(sockfd); exit(0);}

  getdate-udp

/*  Start with the usual includes and declarations.  */#include 
#include
#include
#include
#include
#include
int main(int argc, char *argv[]){ char *host; int sockfd; int len, result; struct sockaddr_in address; struct hostent *hostinfo; struct servent *servinfo; char buffer[128]; if(argc == 1) host = "localhost"; else host = argv[1];/* Find the host address and report an error if none is found. */ hostinfo = gethostbyname(host); if(!hostinfo) { fprintf(stderr, "no host: %s\n", host); exit(1); }/* Check that the daytime service exists on the host. */ servinfo = getservbyname("daytime", "udp"); if(!servinfo) { fprintf(stderr,"no daytime service\n"); exit(1); } printf("daytime port is %d\n", ntohs(servinfo -> s_port));/* Create a UDP socket. */ sockfd = socket(AF_INET, SOCK_DGRAM, 0);/* Construct the address for use with sendto/recvfrom... */ address.sin_family = AF_INET; address.sin_port = servinfo -> s_port; address.sin_addr = *(struct in_addr *)*hostinfo -> h_addr_list; len = sizeof(address); result = sendto(sockfd, buffer, 1, 0, (struct sockaddr *)&address, len); result = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&address, &len); buffer[result] = '\0'; printf("read %d bytes: %s", result, buffer); close(sockfd); exit(0);}

  

转载地址:http://gwwuo.baihongyu.com/

你可能感兴趣的文章
caffe solver
查看>>
Rhel6-heartbeat+lvs配置文档
查看>>
ORACLE分科目统计每科前三名的学生的语句
查看>>
0317复利计算的回顾与总结
查看>>
函数对象
查看>>
最全最新个税计算公式---今天你税了吗?
查看>>
linux shell 正则表达式(BREs,EREs,PREs)差异比较(转,当作资料查)
查看>>
MongoDB--CSharp Driver Quickstart .
查看>>
二分法求平方根(Python实现)
查看>>
使用startActivityForResult方法(转)
查看>>
so在genymotation中错误问题
查看>>
Visual Studio 原生开发的10个调试技巧(二)
查看>>
Windows内核再次出现0Day漏洞 影响win2000到win10所有版本 反病毒软件恐成瞎子
查看>>
H3C品牌刀片系统强势首发
查看>>
【CSS系列】图像映射
查看>>
First blood
查看>>
java 冒泡排序和快速排序 实现
查看>>
SQL存储过程中的几个常见设定SET QUOTED_IDENTIFIER/NOCOUNT/XACT_ABORT ON/OFF
查看>>
第一部分:基础知识(第一章)第一个 Silverlight 手机程序
查看>>
Silverlight与Flash区别之一
查看>>