当前位置:网站首页>socket实现进程间通信
socket实现进程间通信
2022-08-10 05:38:00 【denglin12315】
show me your code:
服务器端:
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#define SERV_TCP_PORT 8000 /* server's port number */
#define MAX_SIZE 80
int main(int argc, char *argv[])
{
int sockfd, newsockfd, clilen;
struct sockaddr_in cli_addr, serv_addr;
int port;
char string[MAX_SIZE];
int len;
/* command line: server [port_number] */
if(argc == 2)
sscanf(argv[1], "%d", &port); /* read the port number if provided */
else
port = SERV_TCP_PORT;
/* open a TCP socket (an Internet stream socket) */
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("can't open stream socket");
exit(1);
}
/* bind the local address, so that the cliend can send to server */
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* 或者使用INADDR_LOOPBACK都可以 */
serv_addr.sin_port = htons(port);
if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("can't bind local address");
exit(1);
}
/* listen to the socket */
listen(sockfd, 5);
for(;;) {
/* wait for a connection from a client; this is an iterative server */
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if(newsockfd < 0) {
perror("can't bind local address");
}
/* read a message from the client */
len = read(newsockfd, string, MAX_SIZE);
/* make sure it's a proper string */
string[len] = 0;
printf("%s\n", string);
close(newsockfd);
}
}
客户端:
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#define SERV_TCP_PORT 8000 /* server's port */
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in serv_addr;
char *serv_host = "localhost";
struct hostent *host_ptr;
int port;
int buff_size = 0;
/* command line: client [host [port]]*/
if(argc >= 2)
serv_host = argv[1]; /* read the host if provided */
if(argc == 3)
sscanf(argv[2], "%d", &port); /* read the port if provided */
else
port = SERV_TCP_PORT;
/* get the address of the host */
if((host_ptr = gethostbyname(serv_host)) == NULL) {
perror("gethostbyname error");
exit(1);
}
if(host_ptr->h_addrtype != AF_INET) {
perror("unknown address type");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr =
((struct in_addr *)host_ptr->h_addr_list[0])->s_addr;
serv_addr.sin_port = htons(port);
/* open a TCP socket */
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("can't open stream socket");
exit(1);
}
/* connect to the server */
if(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("can't connect to server");
exit(1);
}
/* write a message to the server */
write(sockfd, "hello world", sizeof("hello world"));
close(sockfd);
}
Makefile
all: server client
server: server.o
gcc -o server server.o
server.o: server.c
gcc -c server.c
client: client.o
gcc -o client client.o -lnsl
client.o: client.c
gcc -c client.c
边栏推荐
- 浅谈《帧同步网络游戏》之“框架”实现思路
- Talking about 3 common shadow rendering techniques in games (1): plane shadow
- Talking about the realization idea of "frame" of "frame synchronization online game"
- mysql连接报错:Cannot get a connection, pool error Timeout waiting for idle object
- OpenGL学习笔记(LearnOpenGL)-第四部分 着色器
- 动态规划、背包问题 6/25 110-115
- 视差映射:更逼真的纹理细节表现(上):为什么要使用视差映射
- 在Unity中判断游戏物体是否在游戏屏幕范围之内
- UnityShader入门精要-高级光照基础
- Unity2d自动寻路(AI插件)
猜你喜欢
Unity plug-in DOTween User Guide 2 (Brief explanation of Bezier curves)
8个问题轻松掌握Unity前向渲染
浅谈游戏中3种常用阴影渲染技术(1):平面阴影
Unity2d自动寻路(AI插件)
Talking about 3 common shadow rendering techniques in games (1): plane shadow
MySQL 免安装版/解压版的安装与配置(Win & Unix & Linux)
背包问题 c语言版
虚幻5简单第三人称游戏制作文档
从零开始构建Google Protocol Buffer / protobuf 的helloworld工程(超级详细)
R language cluster analysis - code analysis
随机推荐
Unity血条跟随对象
屏幕后期处理之:Sobel算子实现边缘检测
手把手教你改内核源码--sysfs虚拟文件系统1
电镀废水除六价铬
UnityShader入门精要-高级光照基础
Unity object pool implementation
markdown使用技巧
Unity中暂停、继续播放、杀死、正放、倒放Dotween动画
链表、栈、队列
Unity屏幕坐标转世界坐标,鼠标点击获取三维位置
享元模式-缓存池
动态规划、背包问题 6/24 106-110
Easy to master Unity of eight prior to rendering
Myunity框架笔记2
内核性能分析总结
lua循环
电路分析中的电容器的基本知识
手机与雷电模拟器里如何使用YiLu代理?
一小时极速掌握游戏资源远程热更新
mysql分组排序并取各分组前几个数据