TCP相关代码

TCP 基础代码

//tcp_server.c
#include<stdio.h>
#include<error.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<stdlib.h>
#include<error.h>int Startup(char* ip, int port)
{printf("port: %d\n", port);struct sockaddr_in local;local.sin_family = AF_INET;local.sin_port = htons(port);local.sin_addr.s_addr = inet_addr(ip);//设置套接字int sock = socket(AF_INET, SOCK_STREAM, 0);if(sock == -1){perror("socket\n");exit(2);}//绑定if(bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0){perror("bind\n");exit(3);}//监听int ret = listen(sock, 5);if(ret == -1){perror("listen_sock\n");exit(3);}return sock;
}void Server(int new_sock, char* ip, int port)
{char buf[1024];while(1){buf[0] = 0;int s = read(new_sock, buf, sizeof(buf));if(s > 0){buf[s] = 0;printf("[%s, %d] say# %s\n", ip, port, buf);write(new_sock, buf, strlen(buf));}else if(s == 0){printf("[%s, %d] quit\n", ip, port);exit(4);}else{perror("read\n");exit(5);}}
}int main(int argc, char* argv[])
{if(argc != 3){printf("Usage: %s [ip] [port]\n", argv[0]);exit(1);}//此时的套接字已经是监听套接字int listen_sock = Startup(argv[1], atoi(argv[2]));printf("listen and bind is sucessful!\n");//建立连接struct sockaddr_in peer;while(1){socklen_t len = sizeof(peer);//此时的套接字已经是监听套接字int new_sock = accept(listen_sock, (struct sockaddr*)&peer, &len);//此时返回的这个套接字就是负责服务的套接字if(new_sock == -1){printf("accept is error!\n");continue;}char* ip = inet_ntoa(peer.sin_addr);int port = ntohs(peer.sin_port);printf("get a new connet ip is %s, port is %d...\n", ip, port);//服务Server(new_sock, ip, port);//关闭文件描述符}return 0;
}
//tcp_client.c
#include<stdio.h>
#include<error.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<stdlib.h>
#include<error.h>int main(int argc, char* argv[])
{if(argc != 3){printf("Usage %s [ip], [port]\n", argv[0]);exit(1);}//建立套接字int sock = socket(AF_INET, SOCK_STREAM, 0);if(sock == -1){perror("socket\n");exit(2);}struct sockaddr_in server;server.sin_family = AF_INET;server.sin_addr.s_addr = inet_addr(argv[1]);server.sin_port = htons(atoi(argv[2]));//发起连接if(connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1){perror("connect\n");exit(3);}while(1){printf("Please enter#");fflush(stdout);char buf[1024];buf[0] = 0;int s = read(0, buf, sizeof(buf));if(s > 0){buf[s - 1] = 0;if(strcmp(buf, "quit") == 0){printf("client quit!\n");}write(sock, buf, strlen(buf));s = read(sock, buf, sizeof(buf) - 1);buf[s] = 0;printf("Server Echo#%s\n", buf);}}close(sock);return 0;
}

这里写图片描述
这里写图片描述

TCP 多进程多线程版本

//server.c
#include<stdio.h>
#include<error.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<stdlib.h>
#include<error.h>int main(int argc, char* argv[])
{if(argc != 3){printf("Usage %s [ip], [port]\n", argv[0]);exit(1);}//建立套接字int sock = socket(AF_INET, SOCK_STREAM, 0);if(sock == -1){perror("socket\n");exit(2);}struct sockaddr_in server;server.sin_family = AF_INET;server.sin_addr.s_addr = inet_addr(argv[1]);server.sin_port = htons(atoi(argv[2]));//发起连接if(connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1){perror("connect\n");exit(3);}while(1){printf("Please enter#");fflush(stdout);char buf[1024];buf[0] = 0;int s = read(0, buf, sizeof(buf));if(s > 0){buf[s - 1] = 0;if(strcmp(buf, "quit") == 0){printf("client quit!\n");}write(sock, buf, strlen(buf));s = read(sock, buf, sizeof(buf) - 1);buf[s] = 0;printf("Server Echo#%s\n", buf);}}close(sock);return 0;
}
//client.c
#include<stdio.h>
#include<error.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<stdlib.h>
#include<error.h>int main(int argc, char* argv[])
{if(argc != 3){printf("Usage %s [ip], [port]\n", argv[0]);exit(1);}//建立套接字int sock = socket(AF_INET, SOCK_STREAM, 0);if(sock == -1){perror("socket\n");exit(2);}struct sockaddr_in server;server.sin_family = AF_INET;server.sin_addr.s_addr = inet_addr(argv[1]);server.sin_port = htons(atoi(argv[2]));//发起连接if(connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1){perror("connect\n");exit(3);}while(1){printf("Please enter#");fflush(stdout);char buf[1024];buf[0] = 0;int s = read(0, buf, sizeof(buf));if(s > 0){buf[s - 1] = 0;if(strcmp(buf, "quit") == 0){printf("client quit!\n");}write(sock, buf, strlen(buf));s = read(sock, buf, sizeof(buf) - 1);buf[s] = 0;printf("Server Echo#%s\n", buf);}}close(sock);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/383951.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

UVa1635

我们很容易发现最后每一项的系数就是二项式展开&#xff0c;余数和m没有关系就意味着可以被m整除&#xff0c;因此我们就需要求出每一个二项式的系数。但是数据实在太大我们根据唯一分解定理将m和系数都进行分解&#xff0c;然后比较因子的幂。 二项式的计算我们可以根据杨辉三…

几种并发服务器模型的实现:多线程,多进程,select,poll,epoll

http://www.cnblogs.com/wj9012/p/3879605.html 客户端使用select模型&#xff1a; 1 #include <stdio.h>2 #include <stdlib.h>3 #include <string.h>4 #include <errno.h>5 #include <sys/types.h>6 #include <sys/socket.h>7 #include …

哈希表1

1. 初始化 void HashInit(HashTable* ht, HashFunc func) {if(ht NULL || func NULL){return;}ht -> size 0;ht -> func func;int i 0;for(; i < HashMaxSize; i){ht -> data[i].state Empty;} } 2. 哈希表的销毁 void HashDestroy(HashTable* ht) {if(ht…

UVa10820

实质上就是求欧拉函数值 书上有个板子挺好&#xff0c;也不难理解。 #include<cstdio> #include<cstring> #include<algorithm> #include<climits> #include<cctype> #include<queue> #include<set>using namespace std;typedef l…

linux socket 编程(C语言)

https://www.cnblogs.com/x_wukong/p/4541010.html 最近看了一些网络编程的书籍&#xff0c;一直以来总感觉网络编程神秘莫测&#xff0c;其实网络编程入门还是很容易学的&#xff0c;下面这些代码是我在linux下编写的&#xff0c;已经运行过了&#xff0c;编译之后就可以运行了…

哈希表2

哈希表的初始化 void HashInit(HashTable* ht, HashFunc func) {if(ht NULL){return;}ht -> size 0;ht -> func func;size_t i 0;for(; i < MaxSize; i){ht -> data[i] NULL;} } 哈希表的结点创建 HashElem* CreateHashElemNode(KeyType key, ValueType va…

位图

相关数据结构 typedef uint64_t BitmapType;#define BITMAPMAXSIZE 1000 //位图所能容纳的位数typedef struct Bitmap {uint64_t* data;uint64_t capacity; }Bitmap; 初始化 void BitmapInit(Bitmap* bm, uint64_t capacity) {if(bm NULL){return;}//当capacity 100, 2个元…

C++中的inline用法

https://www.cnblogs.com/fnlingnzb-learner/p/6423917.html 1. 引入inline关键字的原因 在c/c中&#xff0c;为了解决一些频繁调用的小函数大量消耗栈空间&#xff08;栈内存&#xff09;的问题&#xff0c;特别的引入了inline修饰符&#xff0c;表示为内联函数。 栈空间就是指…

UVa1262

算是一个模拟吧 #include<cstdio> #include<cstring> #include<algorithm> #include<climits> #include<cctype> #include<queue> #include<set> #include<vector>using namespace std;typedef long long ll; const int INF…

一个Linux下C线程池的实现

http://blog.csdn.net/zouxinfox/article/details/3560891 什么时候需要创建线程池呢&#xff1f;简单的说&#xff0c;如果一个应用需要频繁的创建和销毁线程&#xff0c;而任务执行的时间又非常短&#xff0c;这样线程创建和销毁的带来的开销就不容忽视&#xff0c;这时也是线…

Gym100917 A - Abstract Picture

模拟赛的时候看这道题没有什么头绪&#xff0c;当时有点晕&#xff0c;感冒还没有好&#xff0c;回来以后瞟了一眼题解就明白了&#xff0c;自己实现了一下&#xff0c;也没有很复杂。大概的思路就像拓扑排序一样&#xff0c;需要理解因为涂的是有顺序的&#xff0c;所以我们总…

linux进程通信---几个发送信号的函数(kill,raise,alarm,pause)

http://blog.csdn.net/zzyoucan/article/details/9235685 信号&#xff1a;信号是unix中最古老的进程通信的一种方式&#xff0c;他是软件层次上对中断机制的模拟&#xff0c;是一种异步通信方式&#xff0c;信号可以实现用户空间进程和内核空间进程的交互&#xff0c;内核进程…

数据库以及表的基本操作

一.数据库的操作 create database[if not exists]数据库名; 创建一个名字为company2的使用utf8忽略大小写的数据库 create database company charsetutf8 collate utf8_general_ci; 创建一个数据库区分大小写 create database company1 charsetutf8 collate utf8_general_bin;…

linux 网络编程:使用两线程实现socket同时收发数据

http://blog.csdn.net/li_wen01/article/details/52665505 工作中最近有使用到socket 向客户端同时发送和接收数据&#xff0c;因为是嵌入式linux设备&#xff0c;且要求只能同时一个客户端连接该端口。考虑到节省系统资源&#xff0c;只创建了两个线程分别实现服务端的收发数据…

CF Gym102059 H. Fractions

题目要求找到给定区间的化简后分子分母的和小于1000的数字的个数 我的想法是先找到所有的满足要求的最简分数(总数不超过1e6,而且远小于),然后对询问查找每个最简分数出现的次数. #include<cstdio> #include<cstring> #include<algorithm> #include<cli…

C语言calloc()函数:分配内存空间并初始化

http://c.biancheng.net/cpp/html/134.html 头文件&#xff1a;#include <stdlib.h> calloc() 函数用来动态地分配内存空间并初始化为 0&#xff0c;其原型为&#xff1a; void* calloc (size_t num, size_t size); calloc() 在内存中动态地分配 num 个长度为 siz…

CF Gym100917 C

要找到和为给定值的所有的等比数列. 1肯定是要特判一下. 我的想法是先找到所有等比为1的,等比为1就是将这个数分为相同的一些数,总共就是这个数的所有约数个数减一(有一个约数为1,题目要求至少分成两个数). 对于其他的等比不为1 的,用等比数列的求和公式暴力一发就行了. #i…

多路转接select1

高级IO 通常情况下所有的 IO 都可以分为两步来完成, 第一步等待, 第二步数据搬迁, 为了提高 IO 效率通常所运用的方法就是减少等待的时间 举个钓鱼的例子 现在有五个人张三, 李四, 王五, 赵六, 钱七. 它们五个人来到湖边来钓鱼. 而它们五个人的钓鱼方各不相同. 张三钓鱼方法…

UVa11181

题目要求条件概率,用贝叶斯公式我们很容易得到我们需要求r个人买东西的概率和每个人买东西的条件下其他r-1个人买东西的概率.我们递归枚举,每当枚举到r个人买东西的时候,我们加入到r个人买东西的概率中(全概率公式),然后对于这r个人,除去自己买东西的概率就是其他r-1个人买东西…

Linux epoll模型

http://www.cnblogs.com/venow/archive/2012/11/30/2790031.html 定义&#xff1a; epoll是Linux内核为处理大批句柄而作改进的poll&#xff0c;是Linux下多路复用IO接口select/poll的增强版本&#xff0c;它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利…