实战测试SO_REUSEADDR选项

                设置SO_REUSEADDR选项,对应TCP套接字处于TIME_WAIT状态下的socket可以重复绑定实用,看代码吧。

服务端:

 

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#define MAXLINE 4096



int main()
{
   int listenfd,acceptfd,n;
   socklen_t  clilen;
   char recvbuf[100]={0};
   struct sockaddr_in cliaddr,servaddr;

   listenfd=socket(AF_INET,SOCK_STREAM,0);
   servaddr.sin_family=AF_INET;
   servaddr.sin_port=htons(8888);
   servaddr.sin_addr.s_addr = INADDR_ANY; 

   int iOpt = 1;
   setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &iOpt, sizeof(iOpt));

   bind(listenfd,(struct sockaddr *)&servaddr,sizeof(struct sockaddr_in));
   listen(listenfd,5);

   clilen=sizeof(cliaddr);
   acceptfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);

   

   getchar();
   close(acceptfd);
   close(listenfd);
   return 0;
}

客户端:

 

 

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <netinet/tcp.h>
#define MAXLINE 4096


int main()
{
   int sockfd,ret;
   struct sockaddr_in servaddr;
   char sendbuf[1000]={0};

   sockfd=socket(AF_INET,SOCK_STREAM,0);
   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family=AF_INET;
   servaddr.sin_port=htons(8888);
   servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");



   ret=connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
   printf("ret=%d\n",ret);


   getchar();
   close(sockfd);
   return 0;
}

编译,先启动服务端,再启动客户端。然后先关闭服务端,再关闭客户端,让服务端处理TIME_WAIT状态。

 

 

[mapan@localhost ~]$ netstat -na | grep 8888
tcp        0      0 127.0.0.1:8888              127.0.0.1:49222             TIME_WAIT 

 

此时,在启动服务端,启动客户端,可以看到connect函数成功返回。如果没有设置SO_REUSEADDR选项,则connect函数会返回-1,不信可以试试。

[mapan@localhost test]$ ./client 
ret=0

看,客户端返回成功了,很简单,测试一下就知道了。

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
delphi socket call php socket 例子,可根据需要扩展写成聊天室、网站助理类似淘宝助理,有订单提醒。 <?php //确保在连接客户端时不会超时 set_time_limit(0); $port = 10081 ; $ip = '192.168.1.102'; // create a streaming socket, of type TCP/IP $sock = socket_create ( AF_INET , SOCK_STREAM , SOL_TCP ); // set the option to reuse the port socket_set_option ( $sock , SOL_SOCKET , SO_REUSEADDR , 1 ); // "bind" the socket to the address to "localhost", on port $port // so this means that all connections on this port are now our resposibility to send/recv data, disconnect, etc.. socket_bind ( $sock , $ip , $port ); // start listen for connections socket_listen ( $sock ); // create a list of all the clients that will be connected to us.. // add the listening socket to this list $clients = array( $sock ); while ( true ) { // create a copy, so $clients doesn't get modified by socket_select() $read = $clients ; // get a list of all the clients that have data to be read from // if there are no clients with data, go to next iteration if ( socket_select ( $read , $write = NULL , $except = NULL , 0 ) < 1 ) continue; // check if there is a client trying to connect if ( in_array ( $sock , $read )) { // accept the client, and add him to the $clients array $clients [] = $newsock = socket_accept ( $sock ); // send the client a welcome message socket_write ( $newsock , "这是一个delphi(客户端) socket 与 PHP_socket(服务器) 通信的例子 测试,交流QQ:410578660。 but ill make an exception :)\n" . "There are " .( count ( $clients ) - 1 ). " client(s) connected to the server\n" ); socket_getpeername ( $newsock , $ip ); echo "New client connected: { $ip } \n" ; // remove the listening socket from the clients-with-data array $key = array_search ( $sock , $read ); unset( $read [ $key ]); } // loop through all the clients that have data to read from foreach ( $read as $read_sock ) { // read until newline or 1024 bytes // socket_read while show errors when the client is disconnected, so silence the error messages $data = @ socket_read ( $read_sock , 1024 , PHP_NORMAL_READ ); // check if the client is disconnected if ( $data === false ) { // remove client for $clients array $key = array_search ( $read_sock , $clients ); unset( $clients [ $key ]); echo "client disconnected.\n" ; // continue to the next client to read from, if any continue; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盼盼编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值