C
This is a sample program for socket communication in C language. Here, for the experiment, I am trying to connect from the client to the loopback address 127.0.0.1 so that I can do it on one computer. I referred to a website, but I forgot where it is, so I can't mention it, This is a beginner's memorandum.
sserver
sserver.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
int main() {
int sockfd;
int client_sockfd;
struct sockaddr_in addr;
socklen_t len = sizeof( struct sockaddr_in );
struct sockaddr_in from_addr;
char buf[1024];
//Receive buffer initialization
memset( buf, 0, sizeof( buf ) );
//Socket generation
if( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) {
perror( "socket" );
}
//Stand-by IP / port number setting
addr.sin_family = AF_INET;
addr.sin_port = htons( 1234 );
addr.sin_addr.s_addr = INADDR_ANY;
//bind
if( bind( sockfd, (struct sockaddr *)&addr, sizeof( addr ) ) < 0 ) {
perror( "bind" );
}
printf("waiting for client's connection..\n");
//Waiting for reception
if( listen( sockfd, SOMAXCONN ) < 0 ) {
perror( "listen" );
}
//Waiting for a connect request from a client
if( ( client_sockfd = accept( sockfd, (struct sockaddr *)&from_addr, &len ) ) < 0 ) {
perror( "accept" );
}
//Receive
int rsize;
while( 1 ) {
rsize = recv( client_sockfd, buf, sizeof( buf ), 0 );
if ( rsize == 0 ) {
break;
} else if ( rsize == -1 ) {
perror( "recv" );
} else {
printf( "receive:%s\n", buf );
sleep( 1 );
//response
printf( "send:%s\n", buf );
write( client_sockfd, buf, rsize );
}
}
//Socket closed
close( client_sockfd );
close( sockfd );
return 0;
}
sclient
sclient.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int sockfd;
struct sockaddr_in addr;
//Socket generation
if( (sockfd = socket( AF_INET, SOCK_STREAM, 0) ) < 0 ) {
perror( "socket" );
}
//Destination address / port number setting
addr.sin_family = AF_INET;
addr.sin_port = htons( 1234 );
addr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
//Server connection
connect( sockfd, (struct sockaddr *)&addr, sizeof( struct sockaddr_in ) );
//Data transmission
char send_str[10];
char receive_str[10];
for ( int i = 0; i < 8; i++ ){
sprintf( send_str, "%d", i );
printf( "send:%d\n", i );
if( send( sockfd, send_str, 10, 0 ) < 0 ) {
perror( "send" );
} else {
recv( sockfd, receive_str, 10, 0 );
printf( "receive:%s\n", receive_str );
}
sleep( 1 );
}
//Socket closed
close( sockfd );
return 0;
}
Compile with cc sserver.c -o ss
for the server and cc sclient.c -o sc
for the client.
First, launch sserver. Then launch sclient from another terminal. Then, 8 data will be sent from sclient, the data will be displayed, and the process will end.
Experiment result server side
$ ./ss
waiting for client's connection..
receive:0
send:0
receive:1
send:1
receive:2
send:2
receive:3
send:3
receive:4
send:4
receive:5
send:5
receive:6
send:6
receive:7
send:7
Experimental results Client side
$ ./sc
send:0
receive:0
send:1
receive:1
send:2
receive:2
send:3
receive:3
send:4
receive:4
send:5
receive:5
send:6
receive:6
send:7
receive:7
Python3
I also wrote it in Python. This works the same as the C version. Compared to C, you can write much more clearly. Reference Qiita: [Communication processing by Python] Encode to binary with .encode () and decode to string with .decode (). When encoding utf-8, use .encode ('utf-8').
sserver.py
#!/usr/bin/python3
import socket
host = "127.0.0.1"
port = 1234
serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversock.bind((host,port)) #Bind by specifying IP and PORT
serversock.listen(10) #Listen for a connection (specify the maximum number of queues)
print('Waiting for connections...')
clientsock, client_address = serversock.accept() #Store data when connected
for i in range ( 8 ):
rcvmsg = clientsock.recv(1024).decode()
print('Received : %s' % (rcvmsg))
s_msg = str(i)
print('Send : %s' % s_msg)
clientsock.sendall(s_msg.encode())
clientsock.close()
sclient.py
#!/usr/bin/python3
import socket
host = "127.0.0.1"
port = 1234
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Create an object
client.connect((host, port)) #Now connect to the server
for i in range(8):
message = str(i)
print('Send : %s' % message)
client.send(message.encode()) #Send data
response = client.recv(4096) #Receive should be a power of 2 (not too large)
print('Received: %s' % response.decode())
Recommended Posts