TCP socket/branch [FUNC]
This commit is contained in:
parent
2c7431ed69
commit
fa6265f3d0
10
client.c
10
client.c
|
@ -8,8 +8,8 @@ int main(int argc, char* argv[]){
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Socket information */
|
/* (1) Socket information */
|
||||||
struct sockaddr_in server_addr; // server info
|
struct sockaddr_in server_addr; // server info
|
||||||
int sock; // socket
|
int sock, // socket
|
||||||
int port; // chosen port
|
port; // chosen port
|
||||||
char hostname[24]; // chosen hostname
|
char hostname[24]; // chosen hostname
|
||||||
|
|
||||||
/* (2) Misc. information */
|
/* (2) Misc. information */
|
||||||
|
@ -57,7 +57,7 @@ int main(int argc, char* argv[]){
|
||||||
|
|
||||||
/* (1-) Manage error */
|
/* (1-) Manage error */
|
||||||
if( sock == -1 ){
|
if( sock == -1 ){
|
||||||
perror("erreur création socket");
|
perror("erreur connection server");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ int main(int argc, char* argv[]){
|
||||||
/* [4] Send message
|
/* [4] Send message
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Send message to server */
|
/* (1) Send message to server */
|
||||||
bytes = xsend(sock, to_send, &server_addr);
|
bytes = xwrite(sock, to_send);
|
||||||
|
|
||||||
/* (2) Check if send succesfully */
|
/* (2) Check if send succesfully */
|
||||||
if( bytes == -1 ){
|
if( bytes == -1 ){
|
||||||
|
@ -80,7 +80,7 @@ int main(int argc, char* argv[]){
|
||||||
/* [5] Wait for response
|
/* [5] Wait for response
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Wait for response */
|
/* (1) Wait for response */
|
||||||
bytes = xlisten(sock, &server_addr, to_recv, BUFSIZE);
|
bytes = xread(sock, to_recv, BUFSIZE);
|
||||||
|
|
||||||
/* (2) Check if received successfully (and consistently) */
|
/* (2) Check if received successfully (and consistently) */
|
||||||
if( bytes == -1 ){
|
if( bytes == -1 ){
|
||||||
|
|
83
lib.c
83
lib.c
|
@ -32,8 +32,8 @@ int xbind(const int port){
|
||||||
/* [1] Create xsocket
|
/* [1] Create xsocket
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Create UPD xsocket */
|
/* (1) Create UPD xsocket */
|
||||||
xsocket = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
DEBUG&& debug("xbind", "creating server socket");
|
DEBUG&& debug("xbind", "creating server socket");
|
||||||
|
xsocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
/* (r2-) Manage error */
|
/* (r2-) Manage error */
|
||||||
if( xsocket == -1 ){
|
if( xsocket == -1 ){
|
||||||
|
@ -58,8 +58,8 @@ int xbind(const int port){
|
||||||
/* [3] Bind to port
|
/* [3] Bind to port
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Bind and return -1 if error */
|
/* (1) Bind and return -1 if error */
|
||||||
bound = bind(xsocket, (struct sockaddr*)&addr, sizeof(addr));
|
|
||||||
DEBUG&& debug("xbind", "binding socket to port");
|
DEBUG&& debug("xbind", "binding socket to port");
|
||||||
|
bound = bind(xsocket, (struct sockaddr*)&addr, sizeof(addr));
|
||||||
|
|
||||||
/* (2) Manage error */
|
/* (2) Manage error */
|
||||||
if( bound == -1 ){
|
if( bound == -1 ){
|
||||||
|
@ -72,7 +72,8 @@ int xbind(const int port){
|
||||||
|
|
||||||
/* [4] Mark socket so it will listen for incoming co.
|
/* [4] Mark socket so it will listen for incoming co.
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
DEBUG&& debug("xbind", "make socket listen for co");
|
DEBUG&& debug("xbind", "listen for client");
|
||||||
|
|
||||||
if( listen(xsocket, 1) < 0 ){
|
if( listen(xsocket, 1) < 0 ){
|
||||||
DEBUG&& printf("error\n");
|
DEBUG&& printf("error\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -92,19 +93,21 @@ int xbind(const int port){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int xlisten(const int xsocket, struct sockaddr_in* client, char* buffer, int bufsize){
|
int xlisten(const int xsocket, struct sockaddr_in* client){
|
||||||
|
|
||||||
/* [1] Initialization
|
/* [1] Initialization
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
unsigned int sock_len;
|
unsigned int sock_len;
|
||||||
int clientsock, bytes;
|
int clientsock;
|
||||||
char buf[bufsize] = {'\0'};
|
|
||||||
|
|
||||||
|
|
||||||
/* [2] Wait for client
|
/* [2] Wait for client
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
clientsock = accept(xsocket, (struct sockaddr*) client, &sock_len);
|
/* (1) Wait for incoming client connection */
|
||||||
DEBUG&& debug("xlisten", "listening for client");
|
DEBUG&& debug("xlisten", "listening for client");
|
||||||
|
clientsock = accept(xsocket, (struct sockaddr*) client, &sock_len);
|
||||||
|
|
||||||
|
/* (2) Manage errors */
|
||||||
|
|
||||||
if( clientsock == -1 ){
|
if( clientsock == -1 ){
|
||||||
DEBUG&& printf("error\n");
|
DEBUG&& printf("error\n");
|
||||||
|
@ -113,26 +116,7 @@ int xlisten(const int xsocket, struct sockaddr_in* client, char* buffer, int buf
|
||||||
|
|
||||||
DEBUG&& printf("done\n");
|
DEBUG&& printf("done\n");
|
||||||
|
|
||||||
|
return clientsock;
|
||||||
/* [3] Receive client data
|
|
||||||
=========================================================*/
|
|
||||||
// while there is chunks to read
|
|
||||||
do{
|
|
||||||
|
|
||||||
/* (1) Receive buffer */
|
|
||||||
bytes = recv(clientsock, buf, bufsize);
|
|
||||||
|
|
||||||
/* (2) Copy to target */
|
|
||||||
buffer = realloc(buffer, (strlen(buffer)+strlen(buf)) * sizeof(char );
|
|
||||||
strcat(buffer, buf);
|
|
||||||
|
|
||||||
}while( bytes > 0 );
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,15 +130,15 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
|
||||||
|
|
||||||
/* [0] Initialization
|
/* [0] Initialization
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
int xsocket, bound;
|
int xsocket, bound, connected;
|
||||||
struct hostent *host; // data found by lookup
|
struct hostent *host; // data found by lookup
|
||||||
|
|
||||||
|
|
||||||
/* [1] Create xsocket
|
/* [1] Create xsocket
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Create UPD xsocket */
|
/* (1) Create UPD xsocket */
|
||||||
xsocket = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
DEBUG&& debug("xconnect", "creating client socket");
|
DEBUG&& debug("xconnect", "creating client socket");
|
||||||
|
xsocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
/* (r2-) Manage error */
|
/* (r2-) Manage error */
|
||||||
if( xsocket == -1 ){
|
if( xsocket == -1 ){
|
||||||
|
@ -168,8 +152,8 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
|
||||||
/* [2] Get information by hostname
|
/* [2] Get information by hostname
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Process */
|
/* (1) Process */
|
||||||
host = gethostbyname(hostname);
|
|
||||||
DEBUG&& debug("xconnect", "fetch info by hostname");
|
DEBUG&& debug("xconnect", "fetch info by hostname");
|
||||||
|
host = gethostbyname(hostname);
|
||||||
|
|
||||||
/* (2) Manage error */
|
/* (2) Manage error */
|
||||||
if( host == NULL ){
|
if( host == NULL ){
|
||||||
|
@ -184,8 +168,8 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
|
||||||
/* [3] Set server useful information from fetched info
|
/* [3] Set server useful information from fetched info
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Reset values */
|
/* (1) Reset values */
|
||||||
bzero(serv, sizeof(struct sockaddr_in));
|
|
||||||
DEBUG&& debug("xconnect", "building server info");
|
DEBUG&& debug("xconnect", "building server info");
|
||||||
|
bzero(serv, sizeof(struct sockaddr_in));
|
||||||
|
|
||||||
/* (2) Set server info (ipv4, port) */
|
/* (2) Set server info (ipv4, port) */
|
||||||
serv->sin_family = AF_INET;
|
serv->sin_family = AF_INET;
|
||||||
|
@ -197,6 +181,22 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
|
||||||
DEBUG&& printf("built\n");
|
DEBUG&& printf("built\n");
|
||||||
|
|
||||||
|
|
||||||
|
/* [4] Connect to server
|
||||||
|
=========================================================*/
|
||||||
|
/* (1) Try to connect to given server */
|
||||||
|
DEBUG&& debug("xconnect", "connecting to server");
|
||||||
|
connected = connect(xsocket, (struct sockaddr*) serv, sizeof(*serv));
|
||||||
|
|
||||||
|
/* (2) Manage error */
|
||||||
|
if( connected == -1 ){
|
||||||
|
DEBUG&& printf("error\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG&& printf("done\n");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// return socket
|
// return socket
|
||||||
return xsocket;
|
return xsocket;
|
||||||
}
|
}
|
||||||
|
@ -208,17 +208,16 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize){
|
int xread(const int xsocket, char* buffer, int bufsize){
|
||||||
|
|
||||||
/* [1] Wait for message through xsocket
|
/* [1] Wait for message through xsocket
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Useful data + memory allocation */
|
/* (1) Memory allocation */
|
||||||
socklen_t sock_len = sizeof(struct sockaddr_in);
|
|
||||||
buffer = (char*) realloc(buffer, sizeof(char) * bufsize );
|
buffer = (char*) realloc(buffer, sizeof(char) * bufsize );
|
||||||
|
|
||||||
/* (2) Listen */
|
/* (2) Listen */
|
||||||
DEBUG&& debug("xlisten", "waiting for data");
|
DEBUG&& debug("xread", "waiting for data");
|
||||||
int read = listen(xsocket, 1);
|
int read = recv(xsocket, buffer, bufsize, 0);
|
||||||
|
|
||||||
/* (3) Manage error */
|
/* (3) Manage error */
|
||||||
if( read == -1 ){
|
if( read == -1 ){
|
||||||
|
@ -228,7 +227,7 @@ int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int buf
|
||||||
|
|
||||||
DEBUG&& printf("received\n");
|
DEBUG&& printf("received\n");
|
||||||
|
|
||||||
printf("[xlisten:received] '%s'\n", buffer);
|
DEBUG&& printf("[xread:received] '%s'\n", buffer);
|
||||||
|
|
||||||
|
|
||||||
/* [2] Return number of read characters
|
/* [2] Return number of read characters
|
||||||
|
@ -241,16 +240,16 @@ int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int buf
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int xsend(const int xsocket, char* buffer, struct sockaddr_in* target){
|
int xwrite(const int xsocket, char* buffer){
|
||||||
|
|
||||||
/* [1] Send buffer (message) to target
|
/* [1] Send buffer (message) to target
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Useful data */
|
/* (1) Useful data */
|
||||||
size_t addr_len = sizeof(struct sockaddr_in);
|
unsigned int addr_len = sizeof(struct sockaddr_in);
|
||||||
|
|
||||||
/* (2) Send data */
|
/* (2) Send data */
|
||||||
DEBUG&& debug("xsend", "sending data");
|
DEBUG&& debug("xwrite", "sending data");
|
||||||
int sent = sendto(xsocket, buffer, strlen(buffer)+1, 0, (struct sockaddr*) target, addr_len);
|
int sent = send(xsocket, buffer, strlen(buffer), 0);
|
||||||
|
|
||||||
/* (3) Manage error */
|
/* (3) Manage error */
|
||||||
if( sent == -1 ){
|
if( sent == -1 ){
|
||||||
|
@ -261,7 +260,7 @@ int xsend(const int xsocket, char* buffer, struct sockaddr_in* target){
|
||||||
DEBUG&& printf("sent\n");
|
DEBUG&& printf("sent\n");
|
||||||
|
|
||||||
|
|
||||||
printf("[xsend:sent] '%s'\n", buffer);
|
DEBUG&& printf("[xwrite:sent] '%s'\n", buffer);
|
||||||
|
|
||||||
|
|
||||||
/* [2] Return number of sent characters
|
/* [2] Return number of sent characters
|
||||||
|
|
7
lib.h
7
lib.h
|
@ -31,7 +31,10 @@
|
||||||
int debug(const char* tag, const char* msg);
|
int debug(const char* tag, const char* msg);
|
||||||
|
|
||||||
int xbind(const int port);
|
int xbind(const int port);
|
||||||
|
int xlisten(const int xsocket, struct sockaddr_in* client);
|
||||||
|
|
||||||
int xconnect(const char* hostname, const int port, struct sockaddr_in* serv);
|
int xconnect(const char* hostname, const int port, struct sockaddr_in* serv);
|
||||||
int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize);
|
|
||||||
int xsend(const int xsocket, char* buffer, struct sockaddr_in* target);
|
int xread(const int xsocket, char* buffer, int bufsize);
|
||||||
|
int xwrite(const int xsocket, char* buffer);
|
||||||
#endif
|
#endif
|
||||||
|
|
35
server.c
35
server.c
|
@ -12,7 +12,7 @@ int main(int argc, char* argv[]){
|
||||||
char client_ip[20];
|
char client_ip[20];
|
||||||
int port; // chosen port
|
int port; // chosen port
|
||||||
// Socket
|
// Socket
|
||||||
int sock;
|
int listensock, datasock;
|
||||||
|
|
||||||
|
|
||||||
/* (2) Misc. information */
|
/* (2) Misc. information */
|
||||||
|
@ -54,36 +54,50 @@ int main(int argc, char* argv[]){
|
||||||
/* [3] Create socket
|
/* [3] Create socket
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Create socket */
|
/* (1) Create socket */
|
||||||
sock = xbind(port);
|
listensock = xbind(port);
|
||||||
|
|
||||||
/* (1-) Manage error */
|
/* (1-) Manage error */
|
||||||
if( sock == -1 ){
|
if( listensock == -1 ){
|
||||||
perror("erreur création socket");
|
perror("erreur création socket");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* [4] Wait for client message
|
/* [4] Wait for client
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Listen to data */
|
/* (1) Listen to incoming connection */
|
||||||
bytes = xlisten(sock, &addr_client, to_recv, BUFSIZE);
|
datasock = xlisten(listensock, &addr_client);
|
||||||
|
|
||||||
|
/* (2) Manage error */
|
||||||
|
if( datasock == -1 ){
|
||||||
|
perror("erreur connection client");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* [5] Wait for data
|
||||||
|
=========================================================*/
|
||||||
|
/* (1) wait/read data */
|
||||||
|
bytes = xread(datasock, to_recv, BUFSIZE);
|
||||||
|
|
||||||
/* (2) Manage error */
|
/* (2) Manage error */
|
||||||
if( bytes == -1 ){
|
if( bytes == -1 ){
|
||||||
perror("erreur réception paquet");
|
perror("erreur reception paquet");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get client ip
|
// Get client ip
|
||||||
inet_ntop(AF_INET, &(addr_client.sin_addr), client_ip, 20);
|
inet_ntop(AF_INET, &(addr_client.sin_addr), client_ip, 20);
|
||||||
|
|
||||||
printf("*** received '%s' (%d bytes) from %s:%d\n", to_recv, bytes, client_ip, ntohs(addr_client.sin_port));
|
printf("*** received '%s' (%d bytes) from %s:%d\n", to_recv, bytes, client_ip, ntohs(addr_client.sin_port));
|
||||||
|
|
||||||
/* [5] Send response
|
|
||||||
|
/* [6] Send response
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
/* (1) Send response */
|
/* (1) Send response */
|
||||||
bytes = xsend(sock, to_send, &addr_client);
|
bytes = xwrite(datasock, to_send);
|
||||||
|
|
||||||
/* (2) Manage error */
|
/* (2) Manage error */
|
||||||
if( bytes == -1 ){
|
if( bytes == -1 ){
|
||||||
|
@ -94,7 +108,8 @@ int main(int argc, char* argv[]){
|
||||||
|
|
||||||
/* [6] Close socket
|
/* [6] Close socket
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
close(sock);
|
close(datasock);
|
||||||
|
close(listensock);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue