[PYTHON] [Practice] TCP programming

Introduction

I started doing network programming in the lecture, so I will summarize it instead of a notebook. For the time being, I'm just writing down how I wrote the program, and I have no intention of going into the basic concept at the moment. I plan to edit the posts as needed without dividing them. The programming language is python 2.7 (because it is used in research), c language (lecture designation). However, since it is a part rather than the whole, if you want to refer to it, please draw the library and variable declaration to be read by yourself.

How to proceed

I will write according to the lecture and assignment. Currently, I am writing the following contents. In addition, I will confirm the disclosure of the information of the connection destination server with the teacher.

client

Connect once for the time being! !!

conditions

Implementation (python: github)

ipv4_simple_client.py


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with closing(sock):
  sock.connect((host, port))
  sock.send(b'Hello world')
  print(sock.recv(bufsize))
return

The AF_INET6 address family is indicated by a tuple of length 4 (host, port, flowinfo, scopeid), where flowinfo and scopeid specify the values of sin6_flowinfo and sin6_scope_id in the C struct sockaddr_in6, respectively. For backwards compatibility, the socket module methods allow you to omit sin6_flowinfo and sin6_scope_id, but omitting scopeid can cause problems handling scoped IPv6 addresses.

ipv6_simple_client.py


sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
with closing(sock):
  sock.connect((host, port, flowninfo, scopeid))
  sock.send(b'Hello world')
  print(sock.recv(bufsize))
return

argparse_sample.py


p = argparse.ArgumentParser()

p.add_argument('-b', '--binding', default='127.0.0.1')
p.add_argument('-p', '--port', default=3000, type=int)
args = p.parse_args()

print(args.binding)
print(args.port)

confirm_address_family.py


address_family = socket.AF_INET

try:
  socket.inet_pton(address_family, host)
except:
  address_family = socket.AF_INET6
  try:
    socket.inet_pton(address_family, host)
  except:
    raise Exception , "invalid host. please confirm the value of -b or --binding"
finally:
  print('address_family is %s' %address_family)

Implementation (c language: github)

  1. Create a socket with int socket (int domain, int type, int protocol);.
  2. Specify Address Family and port in the sockaddr_in structure.
  3. Enter the string src with int inet_pton (int af, const char * src, void * dst); Convert to a network address structure of the address family af and copy it to dst.
  4. int connect(int sockfd, const struct sockaddr *addr, connect to the server with socklen_t addrlen);.

ipv4_simple_client.c


int family = AF_INET;
int sock = socket(family, SOCK_STREAM, 0);

struct sockaddr_in server;
server.sin_family = family;
server.sin_port = htons(port);

int pton = inet_pton(family, host, &server.sin_addr);
if (s != 1){
  perror("invalid host.");
  exit(1);
}

int  con = connect(sock, (struct sockaddr *)&server, sizeof(server));
if (con < 0){
  perror("connection failure.");
  exit(1);
}

char buf[244];
memset(buf, 0, sizeof(buf));
read(sock, buf, sizeof(buf));
printf("%s\n", buf);

ipv6_simple_client.c


int family = AF_INET6;
int sock = socket(family, SOCK_STREAM, 0);

struct sockaddr_in6 server;
server.sin6_family = family;
server.sin6_port = htons(port);

int pton = inet_pton(family, host, &server.sin6_addr);
if (s != 1){
  perror("invalid host.");
  exit(1);
}

int  con = connect(sock, (struct sockaddr *)&server, sizeof(server));
if (con < 0){
  perror("connection failure.");
  exit(1);
}

char buf[244];
memset(buf, 0, sizeof(buf));
read(sock, buf, sizeof(buf));
printf("%s\n", buf);

getopt_sample.c


int result;
char *host;
int port;

while((result=getopt(argc,argv,"b:p:"))!=-1){
  switch(result){

  case 'b':
    host = optarg;
    break;
  case 'p':
    port = atoi(optarg);
    break;
  }
}

Recommended Posts

[Practice] TCP programming
numpy practice 1
Poop programming
Graphic programming
Shell programming