2014-06-13 60 views
1

我正在制作一个C++套接字应用程序。为什么“地址已经在使用”发生了

它与Android设备连接。

当我关闭期间接收数据的手机连接..

Framebuffer Ready 
[PCast] Waiting for connection 
[PCast] Connected 
.... 
[PCast] Data arrived 
[PCast] *** Downloading mode *** 
[PCast] Downloading file. Please wait... 
[PCast:sock2file] Downloading... 
[PCast:sock2file] Disconnection detected. Exiting sock2file. 
[PCast] Disconnected 
(Close client socket descriptor) 

[PCast] Waiting for connection 

它看起来像连接成功关闭。但是当我重新启动服务器..

Framebuffer Ready 
[PCast] Socket bind failed: Address already in use 

当我的Android应用程序试图连接(服务器未运行),它说,它是连接到服务器。(数据传输不工作)

当我运行

netstat -anp 

我可以看到我的端口处于CLOSE_WAIT状态。

... 
tcp  1  0 172.30.1.3:5520   172.30.1.2:47144  CLOSE_WAIT -    
tcp  0  0 172.30.1.3:22   172.30.1.1:50598  ESTABLISHED -    
tcp  1  0 172.30.1.3:5520   172.30.1.2:47202  CLOSE_WAIT -  
... 

代码(省略记录,韩国评论...):

#include <sys/types.h> 
#include <sys/stat.h> 
#include <sys/socket.h> 
#include <unistd.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <string> 
#include <ctime> 
#include <cmath> 
#include <linux/fb.h> 
#include <fcntl.h> 
#include <sys/mman.h> 
#include <sys/ioctl.h> 
#include <signal.h> 
#include "imgs.h" 

using std::string; 

#define FBDEV "/dev/fb0" 
int scrsize, width, height; 
int basesize; 

#define CMD_GET_IP_ETH "/home/pi/scripts/ip.sh eth" 
#define CMD_GET_IP_WLN "/home/pi/scripts/ip.sh wlan" 

#define getPercent(total, current) (current/total) * 100 
#define getLargeValue(org, b) (org * b)/320 

short *display; 
short org[320*240]; 

//Convert images to char map 
char strmap[128][5*5]; 

bool debug = false; 

void cons_log(string message){ 
    printf("%s\n", message.c_str()); 
} 

void drawPixel(int x, int y, short col){ 
    if((width * y + x) < width * height){ 
     display[width*y+x] = col; 
    } 
} 

short mkcolor(int r, int g, int b){ 
    return ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3); 
} 

void writeImage(int sx, int sy, char img[], int size){ 
    size *= basesize; 
    for(int y = 0; y < 5; y++) for(int x = 0; x < 5; x++){ 
     int prtx = sx + x * size, prty = sy + y * size; 
     for(int rx = 0; rx < size; rx++) for(int ry = 0; ry < size; ry++){ 
      drawPixel(prtx + rx, prty + ry, img[5*y+x] * 0xffff); 
     } 
    } 
} 

void writeImage(char img[], int size){ 
    int centerX = width/2 - 5 * size/2; 
    int centerY = height/2 - 5 * size/2; 
    writeImage(centerX, centerY, img, size); 
} 

void writeMessage(int x, int y, string s, int size){ 
    size *= basesize; 
    y += height/2 - size * 5; 
    for(int i = 0; i < s.length(); i++){ 
     int prtx = x + i * (size * 5); 
     writeImage(prtx, y, strmap[s.at(i)], size/basesize); 
    } 
} 

void writeMessage(int y, string s, int size){ 
    size *= basesize; 
    int x = width/2 - ((s.length()) * (size * 5))/2; 
    y += height/2 - size * 5; 
    for(int i = 0; i < s.length(); i++){ 
     int prtx = x + i * (size * 5); 
     writeImage(prtx, y, strmap[s.at(i)], size/basesize); 
    } 
} 

void writeMessage(string s, int size){ 
    writeMessage(0, s, size); 
} 

void clear(){ 
    for(int i = 0; i < width * height; i++) display[i] = 0; 
} 

#define img_height(size) size * 5 * basesize 

void printStandBy(bool connected){ 
    clear(); 
    writeMessage("pcast", 2); 
    char buffer[100]; 
    FILE* pipe; 
    pipe = popen("sh /home/pi/scripts/ip.sh eth", "r"); 
    fgets(buffer, 100, pipe); 
    pclose(pipe); 
    writeMessage(img_height(2) + 10, buffer, 1); 
    memset(buffer, 0, 100); 
    pipe = popen("sh /home/pi/scripts/ip.sh wlan", "r"); 
    fgets(buffer, 100, pipe); 
    pclose(pipe); 
    writeMessage(img_height(2) * 2 + 10, buffer, 1); 
    if(connected){ 
     writeMessage(img_height(2) * 3 + 10, "connected", 1); 
    }else{ 
     writeMessage(img_height(2) * 3 + 10, "not connected", 1); 
    } 
} 

void printDownloading(){ 
    clear(); 
    writeImage(IMG_DOWNLOAD, 2); 
    writeMessage(img_height(2) + 10, "downloading", 1); 
} 

int server_sockfd, client_sockfd; 

void endHandler(int signo){ 
    if(client_sockfd != NULL) close(client_sockfd); 
    if(server_sockfd != NULL) close(server_sockfd); 
    exit(0); 
} 

bool sock2file(int socket){ 
    system("sudo rm /tmp/tmp"); 
    int count = 0; 
    int file = open("/tmp/tmp", O_WRONLY | O_CREAT, 0644); 
    int totalBytes = 0; 
    while(true){ 
     char buffer[1024*1024]; 
     int recvBytes = recv(socket, buffer, 1024*1024, MSG_DONTWAIT); 
     if(recvBytes == 0){ 
      return false; 
     }else if(recvBytes == -1){ 
      if(count == 5000){ 
       char send[] = {1, NULL}; 
       write(socket, send, 1); 
       break; 
      }else{ 
       usleep(1000); 
       count++; 
      } 
     }else{ 
      count = 0; 
      char send[] = {0, NULL}; 
      write(socket, send, 1); 
      write(file, buffer, recvBytes); 
      totalBytes += recvBytes; 
      //byte to B/KB/MB/GB 
      int displaySize = totalBytes; 
      char sizeUnit = 'b'; 
      if(displaySize > 1024){ 
       displaySize /= 1024; 
       sizeUnit = 'k'; 
      } 
      if(displaySize > 1024){ 
       displaySize /= 1024; 
       sizeUnit = 'm'; 
      } 
      //Print current status 
      char buf[100]; 
      sprintf(buf, "received %d%c ", displaySize, sizeUnit); 
      string status(buf); 
      writeMessage(0, img_height(2) * 2 + 10, status, 1); 
     } 
    } 
    return true; 
} 

int main(int argc, char **argv){ 
    if(argc == 2){ 
     if(!strcmp(argv[0], "d")) debug = true; 
    } 
    signal(SIGTERM, endHandler); 
    signal(SIGINT, endHandler); 
    memcpy(strmap['0'], IMG_0, 5*5); 
    memcpy(strmap['1'], IMG_1, 5*5); 
    memcpy(strmap['2'], IMG_2, 5*5); 
    memcpy(strmap['3'], IMG_3, 5*5); 
    memcpy(strmap['4'], IMG_4, 5*5); 
    memcpy(strmap['5'], IMG_5, 5*5); 
    memcpy(strmap['6'], IMG_6, 5*5); 
    memcpy(strmap['7'], IMG_7, 5*5); 
    memcpy(strmap['8'], IMG_8, 5*5); 
    memcpy(strmap['9'], IMG_9, 5*5); 
    memcpy(strmap['.'], IMG_DOT, 5*5); 
    memcpy(strmap[':'], IMG_DOBULE_DOT, 5*5); 
    memcpy(strmap['a'], IMG_A, 5*5); 
    memcpy(strmap['b'], IMG_B, 5*5); 
    memcpy(strmap['c'], IMG_C, 5*5); 
    memcpy(strmap['d'], IMG_D, 5*5); 
    memcpy(strmap['e'], IMG_E, 5*5); 
    memcpy(strmap['f'], IMG_F, 5*5); 
    memcpy(strmap['g'], IMG_G, 5*5); 
    memcpy(strmap['h'], IMG_H, 5*5); 
    memcpy(strmap['i'], IMG_I, 5*5); 
    memcpy(strmap['j'], IMG_J, 5*5); 
    memcpy(strmap['k'], IMG_K, 5*5); 
    memcpy(strmap['m'], IMG_M, 5*5); 
    memcpy(strmap['n'], IMG_N, 5*5); 
    memcpy(strmap['l'], IMG_L, 5*5); 
    memcpy(strmap['o'], IMG_O, 5*5); 
    memcpy(strmap['p'], IMG_P, 5*5); 
    memcpy(strmap['q'], IMG_Q, 5*5); 
    memcpy(strmap['r'], IMG_R, 5*5); 
    memcpy(strmap['s'], IMG_S, 5*5); 
    memcpy(strmap['t'], IMG_T, 5*5); 
    memcpy(strmap['u'], IMG_U, 5*5); 
    memcpy(strmap['v'], IMG_V, 5*5); 
    memcpy(strmap['w'], IMG_W, 5*5); 
    memcpy(strmap['x'], IMG_X, 5*5); 
    memcpy(strmap['y'], IMG_Y, 5*5); 
    memcpy(strmap['z'], IMG_Z, 5*5); 
    memcpy(strmap[' '], IMG_SPACE, 5*5); 

    //Framebuffer Setup 
    int fb_fd; 
    fb_fd = open(FBDEV, O_RDWR); 
    if(!fb_fd){ 
     exit(1); 
    } 
    fb_var_screeninfo fvsInfo; 
    if(ioctl(fb_fd, FBIOGET_VSCREENINFO, &fvsInfo)){ 
     exit(1); 
    } 
    width = fvsInfo.xres; 
    height = fvsInfo.yres; 
    scrsize = width * height * 2; 
    basesize = (width + height)/2/100; 
    display = (short *)mmap(0, scrsize, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0); 
    if((int *)display == -1){ 
     exit(1); 
    } 
    printStandBy(false); 
    //Socket setup 
    int state, client_len; 
    int pid; 
    struct sockaddr_in siClient, siServer; 
    state = 0; 
    client_len = sizeof(siClient); 
    if((server_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ 
     exit(0); 
    } 
    bzero(&siServer, sizeof(siServer)); 
    siServer.sin_family = AF_INET; 
    siServer.sin_addr.s_addr = htonl(INADDR_ANY); 
    siServer.sin_port = htons(5520); 
    state = bind(server_sockfd , (struct sockaddr *)&siServer, sizeof(siServer)); 
    if (state == -1){ 
     exit(0); 
    } 
    state = listen(server_sockfd, 5); 
    if (state == -1){ 
     exit(0); 
    } 
    while(1){ 
     client_sockfd = accept(server_sockfd, (struct sockaddr *)&siClient, 
           &client_len); 
     if (client_sockfd == -1){ 
      exit(0); 
     } 
     printStandBy(true); 
     int avg; 
     int orgtime = time(NULL); 
     while(1){ 
      char datainfo[2]; 
      int recvBytes = recv(client_sockfd, datainfo, 2, MSG_WAITALL); 
      if(recvBytes == 0 || recvBytes == -1) break; 
      else{ 
       system("sudo killall omxplayer*"); 
       write(client_sockfd, datainfo, 2); 
       //*** Downloading mode *** 
       printDownloading(); 
       bool status = sock2file(client_sockfd); 
       if(!status) break; 
       printStandBy(true); 
       system("nohup omxplayer /tmp/tmp &"); 
      } 
     } 
     printStandBy(false); 
     close(client_sockfd); 
    } 
} 
  • 该计划并没有坠毁。我退出Ctrl + C(正如你所看到的,有一个处理Ctrl + C,杀死信号......)

  • 我的android应用程序关闭应用程序退出时的连接。

  • 对不起,英文很差。

+0

你的英文很好。你为什么不把韩国的评论留在代码里面,谁可以阅读韩文(我敢肯定你不是唯一一个)? – dfeuer

+0

如果我正确记住我的类,关闭侦听套接字需要长达120秒,所以您需要在每次应用程序重新启动之间留出一些时间。其他套接字类型(不是tcp)没有这个问题iirc。编辑:检查这个答案你可能能够使用相同的标志插座:http://stackoverflow.com/questions/10619952/how-to-completely-destroy-a-socket-connection-in-c –

+0

@LorenzoBoccaccia谢谢。现在看起来好像。 – Gippeumi

回答

4

当远程端关闭连接异常的端口在TIME_WAIT状态捆绑和你不能重用(即重新绑定),除非超时已过期。您可以减少超时,将套接字选项SO_REUSEADDR设置为强制重新使用端口,或等待重新启动服务器时释放端口。

1

在监听套接字上设置SO_REUSEADDR选项将强制操作系统允许您绑定端口,即使它正在使用中。这将解决您的症状,但不是潜在的原因,因此如果真的有多台服务器在运行,则容易出现问题。

如果您的服务器在从客户端到达FIN之前启动了在TCP套接字上发送FIN,那么TCP状态机会强制它转换到TIME_WAIT状态。在你的情况,你可能会关闭你的服务器与CtrlC客户端实际关闭连接之前,因此服务器启动FIN。

由于您正在强制关闭服务器,因此您可能宁愿在TCP套接字上发出RESET。在大多数TCP堆栈上,这是通过启用SO_LINGER选项来实现的,其超时值为0。你也可以设置这种情况,当你已经超时等待客户发送一些东西。

+0

我试图用SO_LINGER超时0。但它仍然表示,在使用地址我看到这种情况下,此消息:当我结束发送大文件时的连接(小文件发出后) – Gippeumi

+0

你只做你要强行关闭连接的情况下SO_LINGER的解决方案。这是不是你让所有的时间的选项。在你在你的服务器发布的代码的强制条件中[按Ctrl] [C]和超时后(的5张秒钟的等待)。在sock2file() – jxh

+0

5秒超时?它是关于数据传输。如果有5秒钟没有更多的数据,它播放下载的视频。 – Gippeumi

相关问题