begin program

This commit is contained in:
asus
2023-02-06 18:43:40 +01:00
parent 72f305d8eb
commit a8bd2210e0
2 changed files with 80 additions and 57 deletions

BIN
a.out Executable file

Binary file not shown.

View File

@@ -1,64 +1,87 @@
//#include <errno.h>
//#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
// write
// close
// select
// socket
// accept // accept
// listen
// send
// recv
// bind
// strstr
// malloc
// realloc
// free
// calloc
// bzero
// atoi // atoi
// sprintf // bind
// strlen // bzero
// calloc
// close
// exit // exit
// strcpy // free
// strcat // listen
// malloc
// memset // memset
// realloc
// recv
// select
// send
// socket
// sprintf
// strcat
// strcpy
// strlen
// strstr
// write
void putstr(int fd, char *str) {
int main() { write(fd, str, strlen(str));
int sockfd, connfd, len; }
struct sockaddr_in servaddr, cli;
void error(char *str, int code) {
// socket create and verification putstr(2, str);
sockfd = socket(AF_INET, SOCK_STREAM, 0); exit(code);
if (sockfd == -1) { }
printf("socket creation failed...\n");
exit(0); int main(int ac, char **av) {
} int port;
else int sockfd;
printf("Socket successfully created..\n"); // int connfd;
bzero(&servaddr, sizeof(servaddr)); // int len;
struct sockaddr_in servaddr;
// assign IP, PORT // struct cli;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1 if (ac < 1)
servaddr.sin_port = htons(8081); error("Wrong number of arguments\n", 1);
port = atoi(av[1]);
// Binding newly created socket to given IP and verification if (port == -1)
if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) { error("Fatal error\n", 1);
printf("socket bind failed...\n");
exit(0); // socket create and verification
} sockfd = socket(AF_INET, SOCK_STREAM, 0);
else if (sockfd == -1)
printf("Socket successfully binded..\n"); error("socket creation failed...\n", 0);
if (listen(sockfd, 10) != 0) { else
printf("cannot listen\n"); putstr(1, "Socket successfully created..\n");
exit(0); bzero(&servaddr, sizeof(servaddr));
}
len = sizeof(cli); // assign IP, PORT
connfd = accept(sockfd, (struct sockaddr *)&cli, &len); servaddr.sin_family = AF_INET;
if (connfd < 0) { servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
printf("server acccept failed...\n"); servaddr.sin_port = htons(8081);
exit(0);
} // // Binding newly created socket to given IP and verification
else // if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) {
printf("server acccept the client...\n"); // printf("socket bind failed...\n");
// exit(0);
// }
// else
// printf("Socket successfully binded..\n");
// if (listen(sockfd, 10) != 0) {
// printf("cannot listen\n");
// exit(0);
// }
// len = sizeof(cli);
// connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
// if (connfd < 0) {
// printf("server acccept failed...\n");
// exit(0);
// }
// else
// printf("server acccept the client...\n");
} }