#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
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;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。