source: launchers/macosx/obj-cpp/portcheck.h @ f9fbc76

Last change on this file since f9fbc76 was f9fbc76, checked in by meeh <meeh@…>, 2 years ago

Code changes:

  • Port check to see if i2p is already running.
  • All buttons in the menu should work now.
  • Rewrote some C++ to Objective-C, which fits better with the integration.
  • Misc cleanup.
  • Made the launch of router repeatable.
  • Property mode set to 100644
File size: 1.4 KB
Line 
1#pragma once
2
3#include <stdio.h>
4#include <unistd.h>
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <arpa/inet.h>
8#include <errno.h>
9
10typedef struct sockaddr *sad; /* A necesary dummy typedef */
11
12
13int port_check(int portNum=7657)
14{
15int sock; /* Socket that will be bind */
16struct sockaddr_in sin; /* Address Structure */
17
18/* Create the socket */
19/* PF_INET is the option for make a TCP socket.
20        You can try "man socket" for more info */
21sock = socket( PF_INET, SOCK_STREAM, 0 );
22
23/* The socket creation failed */
24if ( 0 > sock ) {
25  perror( "socket" );
26  return ( -1 );
27}
28
29/* Address */
30sin.sin_family = AF_INET;
31sin.sin_port = htons( portNum ); /* htons() convert the number
32                                                      to big endian */
33sin.sin_addr.s_addr = INADDR_ANY;
34
35/* We bind the socket to the port PORT to check if
36          the port is in use */
37if ( 0 > bind( sock, (sad)&sin, sizeof( sin ) ) ) {
38  /* Bind failed, now we can check if the address is
39                in use */
40
41  if ( EADDRINUSE == errno ) {
42  /* We put the code necesary to manage this case */
43  printf( "The TCP port %d is in use.\n", portNum );
44  }
45
46  else
47  /* If the error were other than EADDRINUSE, we print it */
48  perror( "bind" );
49
50  return 1;
51}
52
53/* If we arrive to this point, the port weren't in use and
54          we have it attached to our program. We can close
55          the socket or use it */
56close( sock ); /* Close the socket */
57
58return 0;
59}
Note: See TracBrowser for help on using the repository browser.