一、网络编程
1、socket服务端代码实现(无连接客户端)
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
//#include <linux/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
int main()
{
int s_fd;
//1.socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("socked");
exit(-1);
}
struct sockaddr_in s_addr;
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(8989);
//s_addr.sin_addr.s_addr = "127.0.0.1"; (error)
inet_aton("192.168.22.148",&s_addr.sin_addr); //虚拟机linux的ip地址
//2.bind
bind(s_fd, (struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen
listen(s_fd,10);
//4.accept
int c_fd = accept(s_fd,NULL,NULL);
//5.read
//6.write
printf("connect\n");
while(1);
return 0;
}
输出结果:
window系统
6、socket服务端代码实现(连接客户端)
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
//#include <linux/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
//int socket(int domain, int type, int protocol);
//int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
int main()
{
int s_fd;
int n_read;
char readBuf[128];//定义一个数组
char *msg = "I got your message";
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in)); //一般来说先清空空间数据,再配置。避免结构体里面有杂乱数据
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("socked");
exit(-1);
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(8988);
//s_addr.sin_addr.s_addr = "127.0.0.1"; (不是这样写的)
inet_aton("192.168.22.148",&s_addr.sin_addr);
//2.bind
bind(s_fd, (struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen
listen(s_fd,10);
//4.accept
int clen = sizeof(struct sockaddr_in);//长度
int c_fd = accept(s_fd,(struct sockaddr *)&c_addr, &clen);
if(c_fd == -1){
perror("accept");
}
//成功则打印客户端ip地址
printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr)); //inet_ntoa()函数,把网络格式的 IP 地址转为字符串形式
//==================服务端与客户端之间数据交互======================================
//5.read
n_read = read(c_fd,readBuf,128);//把客户端的内容读到readBuf
if(n_read == -1){
perror("read");
}else{
printf("get message: %d,%s\n",n_read,readBuf);//打印readBuf中内容
}
//6.write
write(c_fd,msg,strlen(msg));//写入操作
//===============================================================================
return 0;
}
输出结果:
7、socket客户端代码实现
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main()
{
int c_fd;
int n_read;
char readBuf[128];
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.socket
c_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1){
perror("socked");
exit(-1);
}
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(8988);
//s_addr.sin_addr.s_addr = "127.0.0.1"; //(不是这样写的)
inet_aton("192.168.22.148,&c_addr.sin_addr);
//2.connect
if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr_in)) == -1){
perror("connect");
exit(-1); //连接不到服务端会阻塞,出错直接跳出程序,以免搞崩代码
}
//3.send
write(c_fd,msg,128);
//==================读取服务端的信息===============================
//4.read
n_read = read(c_fd,readBuf,128);
if(n_read == -1){
perror("read");
}else{
printf("get message from server: %d ,%s\n",n_read,readBuf);
}
//================================================================
return 0;
}
8、实现双方聊天
- server.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc,char **argv)
{
int s_fd;
int c_fd;
int n_read;
char readBuf[128];
char *msg = "I got your message";
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
memset(&c_addr,0,sizeof(struct sockaddr_in));
if(argc != 3){ //判断一下参数,否则运行参数没写对的时候,出现段错误不知道是哪里搞错。
printf("param isn't right\n");
}
//1.socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("socked");
exit(-1);
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2]));
//s_addr.sin_addr.s_addr = "127.0.0.1"; (error)
inet_aton(argv[1],&s_addr.sin_addr);
//2.bind
bind(s_fd, (struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen
listen(s_fd,10);
//4.accept
int clen = sizeof(struct sockaddr_in);
while(1){
c_fd = accept(s_fd,(struct sockaddr *)&c_addr, &clen);
if(c_fd == -1){
perror("accept");
}
printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));
if(fork() == 0){
//5.read
n_read = read(c_fd,readBuf,128);
if(n_read == -1){
perror("read");
}else{
printf("get message: %d ,%s\n",n_read,readBuf);
}
//6.write
write(c_fd,msg,strlen(msg));
break; //子进程做完一次就退出,父进程继续接收数据
}
}
return 0;
}
~
- client.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc,char **argv)
{
int c_fd;
int n_read;
char readBuf[128];
char *msg = "from client msg";
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
if(argc != 3){ //判断一下参数,否则参数没写对的时候,出现段错误不知道是哪里搞错。
printf("param isn't right\n");
}
//1.socket
c_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1){
perror("socket");
exit(-1);
}
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2]));
//s_addr.sin_addr.s_addr = "127.0.0.1"; (error)
inet_aton(argv[1],&c_addr.sin_addr);
// memset(&c_addr,0,sizeof(struct sockaddr_in)); (error:清空位置不对)
//2.connect
if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr_in)) == -1){
perror("connect");
exit(-1);
}
//3.send
int n_write = write(c_fd,msg,strlen(msg));
if(n_write == -1){
perror("write:");
}
//4.read
n_read = read(c_fd,readBuf,128);
if(n_read == -1){
perror("read");
}else{
printf("get message from server: %d ,%s\n",n_read,readBuf);
}
return 0;
}
输出结果:
9、多方消息收发
- server.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int mark=0;
int s_fd;
int c_fd;
int n_read;
int n_write;
char readBuf[128];
char returnMsg[128]={0};
struct sockaddr_in c_addr;
struct sockaddr_in s_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
memset(&s_addr,0,sizeof(struct sockaddr_in));//数据清空
if(argc != 3){
printf("参数出错\n");
exit(-1);
}
//1.socket int socket(int domain, int type, int protocol);
s_fd=socket(AF_INET,SOCK_STREAM,0);
//ipv4 tcp协议
if(s_fd == -1){
printf("创建socket失败");
perror("socket:");
exit(-1);
}
//2.bind int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
s_addr.sin_family=AF_INET;//ipv4
s_addr.sin_port=htons(atoi(argv[2]));//端口号,选择5000以上。honts返回网络字节序,atoi(argv[2])防止端口被占用
//int inet_aton(const char *cp, struct in_addr *inp)
inet_aton(argv[1],&s_addr.sin_addr);//转换为网络能识别的格式
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen int listen(int sockfd, int backlog);
listen(s_fd,10);//监听10个连接
//4.accept int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int client=sizeof(struct sockaddr_in);
while(1){//不断接收客户端
c_fd=accept(s_fd,(struct sockaddr *)&c_addr,&client);
if(c_fd == -1){
printf("连接失败\n");
perror("accept:");
exit(-1);
}
printf("客户端的ip:%s\n",inet_ntoa(c_addr.sin_addr)); //把网络格式的ip地址打印成字符串格式
mark++;
if(fork() == 0){
if(fork() == 0){
while(1){
sprintf(returnMsg,"欢迎第%d号客户端",mark);
//6.write ssize_t write(int fd, const void *buf, size_t count);
n_write=write(c_fd,returnMsg,strlen(returnMsg));
sleep(20);
}
}
while(1){
//5.read ssize_t read(int fd, void *buf, size_t count);
memset(readBuf,0,sizeof(readBuf));
n_read=read(c_fd,readBuf,128);
if(n_read == -1){
perror("read:");
}else{
printf("得到%d号的消息:%s\n",mark,readBuf);
}
}
}
}
return 0;
}
- client.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
int c_fd;
int n_read;
int n_write;
int c_connect;
char readBuf[128];
char returnMsg[128]={0};
char *quit="quit";
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));//数据清空
if(argc != 3){
printf("参数出错\n");
exit(-1);
}
//1.socket int socket(int domain, int type, int protocol);
c_fd=socket(AF_INET,SOCK_STREAM,0);
//ipv4 tcp协议
if(c_fd == -1){
printf("创建socket失败");
perror("socket:");
exit(-1);
}
c_addr.sin_family=AF_INET;//ipv4
c_addr.sin_port=htons(atoi(argv[2]));//端口号,选择5000以上。honts返回网络字节序
//int inet_aton(const char *cp, struct in_addr *inp)
inet_aton(argv[1],&c_addr.sin_addr);//转换为网络能识别的格式
//2.connect int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
c_connect=connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr));
if(c_connect == -1){
printf("连接失败\n");
perror("connect:");
}
while(1){
if(fork() == 0){
while(1){//不断写入
memset(returnMsg,0,sizeof(returnMsg));
printf("请输入:\n");
gets(returnMsg);
//3.write/send ssize_t write(int fd, const void *buf, size_t count);
n_write=write(c_fd,returnMsg,strlen(returnMsg));
if(strcmp(quit,returnMsg) == 0){//如果输入quit则客户端就退出
exit(0);
}
}
}
while(1){//不断读取
memset(readBuf,0,sizeof(readBuf));//不断清空数据防止数据重复出现
//4.read ssize_t read(int fd, void *buf, size_t count);
n_read=read(c_fd,readBuf,128);
if(n_read == -1){
perror("read:");
}else{
printf("来自服务端的消息:%s\n",readBuf);
}
}
//5.close int close(int fd);
close(c_fd);
}
return 0;
}
二、往期文章
1、入门Linux系统编程–文件
2、入门Linux系统编程–进程
3、入门Linux系统编程–线程
4、入门Linux系统编程–进程间通信
5、入门Linux系统编程–网络编程
版权声明:本文为mantouyouyou原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。