#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
int
main (int argc, char **argv)
{
int sockfd, new_fd;
struct sockaddr_in my_addr, their_addr;
int sin_size, numbytes;
char buff[100];
if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0)))
{
perror ("socket\n");
exit (1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons (2323);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero (&(my_addr.sin_zero), 8);
if (-1 == bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)))
{
perror ("bind\n");
exit (1);
}
if (-1 == listen(sockfd, 10))
{
perror ("listen\n");
exit (1);
}
while (1)
{
sin_size = sizeof(struct sockaddr_in);
printf ("server %d is run\n", getpid());
if (-1 == (new_fd = accept(sockfd, (struct sockaddr *)&their_addr,
&sin_size)))
{
perror ("accept\n");
exit (1);
}
if (!fork())
{
bzero(buff, 100);
if (-1 ==(numbytes = recv(new_fd, buff, 100, 0)))
{
perror ("recv\n");
exit (1);
}
printf ("pid %d:%s\n", getpid(), buff);
if (-1 == send(new_fd, buff, strlen(buff), 0))
{
perror ("send\n");
}
close (new_fd);
exit (0);
}
close (new_fd);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
LinuxTCP/IP通信实例
共2个文件
c:2个
5星 · 超过95%的资源 需积分: 10 47 下载量 44 浏览量
2008-09-25
21:53:32
上传
评论
收藏 1KB RAR 举报
温馨提示
包含两个文件server.c,client.c,直接用gcc编译生成可执行文件,分别在不同的linux机器上运行。
资源推荐
资源详情
资源评论
收起资源包目录
Linux_tcpip.rar (2个子文件)
client.c 1KB
server.c 1KB
共 2 条
- 1
chenhj_wo
- 粉丝: 9
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页