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.
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.
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
`-p``` and
--port```, and the host specifications are `` -b``` and ``
--binding(-h is It was filled with help, and the command when starting the IP server in rails was `` `rails s -b <IP Address>
...)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)
socket.inet_pton (address_family, ip_string)
`, if the IP address is invalid, socket IPv4 or IPv6 was determined by using the occurrence of .error.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)
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);
`-p```, and the host specification is
`-b```.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;
}
}