#ident "@(#)tcp_server.c,v 6.1 1993/08/21 02:17:17 stevea Exp - STREAMware TCP/IP source"
/* SCCS IDENTIFICATION */
/*
* copyright (C) 1986, 1988 by Larry McVoy.
* MUST be distributed in source form only.
*/
/* server.c 1.0 - main */
# include <stdio.h>
# include <signal.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <sys/time.h>
# include <netinet/in.h>
# include <netdb.h>
struct sockaddr_in sin = { AF_INET }; /* the rest is null */
/*
* try to see what we can say.
*/
main(argc, argv)
int argc ;
char **argv;
{
char buf[2048];
int seq, namelen, newsock, sock;
if ( argc<2 )
{
printf ( "usage:%s port_number\n", argv[0] ) ;
exit ( -1 ) ;
}
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
perror("socket");
exit(1);
}
printf ( "socket()...\n" ) ;
sin.sin_port = htons ( atoi(argv[1]) ) ;
if (bind(sock, &sin, sizeof(sin)) < 0)
{
perror("bind");
exit(2);
}
printf ( "bind() port:%d ok.\n", sin.sin_port ) ;
namelen = sizeof(sin);
if (getsockname(sock, &sin, &namelen) < 0)
{
perror("getsockname");
exit(3);
}
printf ( "getsockname()...\n" ) ;
printf("Server bound to port %u 0x%x \n", ntohs(sin.sin_port),sin.sin_port);
if (fork())
{
exit(0);
}
printf ( "father process exit..\n" ) ;
if (listen(sock, 5) < 0)
{
perror("listen");
exit(4);
}
printf ( "listen()...\n" ) ;
namelen = sizeof(sin);
if ((newsock = accept(sock, &sin, &namelen)) < 0)
{
perror("accept");
exit(5);
}
printf ( "accept()...\n" ) ;
/*
* At this point we have a connection much like a pipe. You may read and
* write from newsock and that is talking to sock on the client side.
*/
seq = 0;
sleep(5);
for (;;)
{
int netint = htonl(seq);
printf ( "send %d\n", seq ) ;
/***test send-Q *
memset ( buf, 0, 2048 ) ;
write(newsock, buf, 2048) ;
****/
if ( write(newsock, &netint, 4)< 0 )
{
printf ( "server send fail. exit!\n" ) ;
exit ( -1 ) ;
}
seq++;
sleep(3);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
tcp.rar_linux tcp client_tcp serve
共6个文件
c:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 134 浏览量
2022-09-23
11:14:07
上传
评论
收藏 6KB RAR 举报
温馨提示
tcp 聊天程序 基于LINUX 实现SERVE CLIENT功能
资源详情
资源评论
资源推荐
收起资源包目录
tcp.rar (6个子文件)
tcp
tcp_server.c 2KB
srv.c 2KB
talk_srv.c 2KB
tcp_client.c 2KB
clt.c 2KB
talk_clt.c 2KB
共 6 条
- 1
林当时
- 粉丝: 100
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0