TCP socket/branch [FUNC]

This commit is contained in:
xdrm-brackets 2017-02-08 11:44:10 +01:00
parent 2c7431ed69
commit fa6265f3d0
9 changed files with 76 additions and 59 deletions

BIN
client

Binary file not shown.

View File

@ -8,8 +8,8 @@ int main(int argc, char* argv[]){
=========================================================*/
/* (1) Socket information */
struct sockaddr_in server_addr; // server info
int sock; // socket
int port; // chosen port
int sock, // socket
port; // chosen port
char hostname[24]; // chosen hostname
/* (2) Misc. information */
@ -57,7 +57,7 @@ int main(int argc, char* argv[]){
/* (1-) Manage error */
if( sock == -1 ){
perror("erreur création socket");
perror("erreur connection server");
exit(1);
}
@ -65,7 +65,7 @@ int main(int argc, char* argv[]){
/* [4] Send message
=========================================================*/
/* (1) Send message to server */
bytes = xsend(sock, to_send, &server_addr);
bytes = xwrite(sock, to_send);
/* (2) Check if send succesfully */
if( bytes == -1 ){
@ -80,7 +80,7 @@ int main(int argc, char* argv[]){
/* [5] 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) */
if( bytes == -1 ){

BIN
client.o

Binary file not shown.

83
lib.c
View File

@ -32,8 +32,8 @@ int xbind(const int port){
/* [1] Create xsocket
=========================================================*/
/* (1) Create UPD xsocket */
xsocket = socket(AF_INET, SOCK_STREAM, 0);
DEBUG&& debug("xbind", "creating server socket");
xsocket = socket(AF_INET, SOCK_STREAM, 0);
/* (r2-) Manage error */
if( xsocket == -1 ){
@ -58,8 +58,8 @@ int xbind(const int port){
/* [3] Bind to port
=========================================================*/
/* (1) Bind and return -1 if error */
bound = bind(xsocket, (struct sockaddr*)&addr, sizeof(addr));
DEBUG&& debug("xbind", "binding socket to port");
bound = bind(xsocket, (struct sockaddr*)&addr, sizeof(addr));
/* (2) Manage error */
if( bound == -1 ){
@ -72,7 +72,8 @@ int xbind(const int port){
/* [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 ){
DEBUG&& printf("error\n");
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
=========================================================*/
unsigned int sock_len;
int clientsock, bytes;
char buf[bufsize] = {'\0'};
int clientsock;
/* [2] Wait for client
=========================================================*/
clientsock = accept(xsocket, (struct sockaddr*) client, &sock_len);
/* (1) Wait for incoming client connection */
DEBUG&& debug("xlisten", "listening for client");
clientsock = accept(xsocket, (struct sockaddr*) client, &sock_len);
/* (2) Manage errors */
if( clientsock == -1 ){
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");
/* [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 );
return clientsock;
}
@ -146,15 +130,15 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
/* [0] Initialization
=========================================================*/
int xsocket, bound;
int xsocket, bound, connected;
struct hostent *host; // data found by lookup
/* [1] Create xsocket
=========================================================*/
/* (1) Create UPD xsocket */
xsocket = socket(AF_INET, SOCK_STREAM, 0);
DEBUG&& debug("xconnect", "creating client socket");
xsocket = socket(AF_INET, SOCK_STREAM, 0);
/* (r2-) Manage error */
if( xsocket == -1 ){
@ -168,8 +152,8 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
/* [2] Get information by hostname
=========================================================*/
/* (1) Process */
host = gethostbyname(hostname);
DEBUG&& debug("xconnect", "fetch info by hostname");
host = gethostbyname(hostname);
/* (2) Manage error */
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
=========================================================*/
/* (1) Reset values */
bzero(serv, sizeof(struct sockaddr_in));
DEBUG&& debug("xconnect", "building server info");
bzero(serv, sizeof(struct sockaddr_in));
/* (2) Set server info (ipv4, port) */
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");
/* [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 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) Useful data + memory allocation */
socklen_t sock_len = sizeof(struct sockaddr_in);
/* (1) Memory allocation */
buffer = (char*) realloc(buffer, sizeof(char) * bufsize );
/* (2) Listen */
DEBUG&& debug("xlisten", "waiting for data");
int read = listen(xsocket, 1);
DEBUG&& debug("xread", "waiting for data");
int read = recv(xsocket, buffer, bufsize, 0);
/* (3) Manage error */
if( read == -1 ){
@ -228,7 +227,7 @@ int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int buf
DEBUG&& printf("received\n");
printf("[xlisten:received] '%s'\n", buffer);
DEBUG&& printf("[xread:received] '%s'\n", buffer);
/* [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) Useful data */
size_t addr_len = sizeof(struct sockaddr_in);
unsigned int addr_len = sizeof(struct sockaddr_in);
/* (2) Send data */
DEBUG&& debug("xsend", "sending data");
int sent = sendto(xsocket, buffer, strlen(buffer)+1, 0, (struct sockaddr*) target, addr_len);
DEBUG&& debug("xwrite", "sending data");
int sent = send(xsocket, buffer, strlen(buffer), 0);
/* (3) Manage error */
if( sent == -1 ){
@ -261,7 +260,7 @@ int xsend(const int xsocket, char* buffer, struct sockaddr_in* target){
DEBUG&& printf("sent\n");
printf("[xsend:sent] '%s'\n", buffer);
DEBUG&& printf("[xwrite:sent] '%s'\n", buffer);
/* [2] Return number of sent characters

7
lib.h
View File

@ -31,7 +31,10 @@
int debug(const char* tag, const char* msg);
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 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

BIN
lib.o

Binary file not shown.

BIN
server

Binary file not shown.

View File

@ -12,7 +12,7 @@ int main(int argc, char* argv[]){
char client_ip[20];
int port; // chosen port
// Socket
int sock;
int listensock, datasock;
/* (2) Misc. information */
@ -54,36 +54,50 @@ int main(int argc, char* argv[]){
/* [3] Create socket
=========================================================*/
/* (1) Create socket */
sock = xbind(port);
listensock = xbind(port);
/* (1-) Manage error */
if( sock == -1 ){
if( listensock == -1 ){
perror("erreur création socket");
exit(1);
}
/* [4] Wait for client message
/* [4] Wait for client
=========================================================*/
/* (1) Listen to data */
bytes = xlisten(sock, &addr_client, to_recv, BUFSIZE);
/* (1) Listen to incoming connection */
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 */
if( bytes == -1 ){
perror("erreur réception paquet");
perror("erreur reception paquet");
exit(1);
}
// Get client ip
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));
/* [5] Send response
/* [6] Send response
=========================================================*/
/* (1) Send response */
bytes = xsend(sock, to_send, &addr_client);
bytes = xwrite(datasock, to_send);
/* (2) Manage error */
if( bytes == -1 ){
@ -94,7 +108,8 @@ int main(int argc, char* argv[]){
/* [6] Close socket
=========================================================*/
close(sock);
close(datasock);
close(listensock);
return 0;
}

BIN
server.o

Binary file not shown.