UnixSocket编程:TCP服务端
#include
创新互联是一家专注于成都网站制作、网站设计与策划设计,清河网站建设哪家好?创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:清河等地区。清河做网站价格咨询:13518219792
#include
#include
#include
#include
int main(int argc,char * argv[])
{
int serv_sock,clnt_sock;
char msg[] = "Hello SZT !~";
struct sockaddr_in serv_adr,clnt_adr;
socklen_t clnt_adr_sz;
if(argc != 2)
{
printf("运行时请输入端口号!~\n");
exit(1);
}
serv_sock = socket(PF_INET,SOCK_STREAM,0);
memset(&serv_adr,0,sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_adr.sin_port = htons(atoi(argv[1]));
bind(serv_sock,(struct sockaddr *)&serv_adr,sizeof(serv_adr));
if(listen(serv_sock,5) != -1)
{
printf("listen();正在等待客户端连接,端口:%s\n",argv[1]);
}
clnt_adr_sz = sizeof(clnt_adr);
clnt_sock = accept(serv_sock,(struct sockaddr *)&serv_adr,&clnt_adr_sz);
if(clnt_sock != -1)
{
printf("accept();已经连接客户端\n");
}
write(clnt_sock,msg,sizeof(msg));
printf("write();已经发送数据:%s\n",msg);
close(clnt_sock);
close(serv_sock);
return 0;
}
本文名称:UnixSocket编程:TCP服务端
标题网址:http://hbruida.cn/article/ggpdec.html