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,40 +1,63 @@
//#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) {
write(fd, str, strlen(str));
}
int main() { void error(char *str, int code) {
int sockfd, connfd, len; putstr(2, str);
struct sockaddr_in servaddr, cli; exit(code);
}
int main(int ac, char **av) {
int port;
int sockfd;
// int connfd;
// int len;
struct sockaddr_in servaddr;
// struct cli;
if (ac < 1)
error("Wrong number of arguments\n", 1);
port = atoi(av[1]);
if (port == -1)
error("Fatal error\n", 1);
// socket create and verification // socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) { if (sockfd == -1)
printf("socket creation failed...\n"); error("socket creation failed...\n", 0);
exit(0);
}
else else
printf("Socket successfully created..\n"); putstr(1, "Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr)); bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT // assign IP, PORT
@@ -42,23 +65,23 @@ int main() {
servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1 servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
servaddr.sin_port = htons(8081); servaddr.sin_port = htons(8081);
// Binding newly created socket to given IP and verification // // Binding newly created socket to given IP and verification
if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) { // if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n"); // printf("socket bind failed...\n");
exit(0); // exit(0);
} // }
else // else
printf("Socket successfully binded..\n"); // printf("Socket successfully binded..\n");
if (listen(sockfd, 10) != 0) { // if (listen(sockfd, 10) != 0) {
printf("cannot listen\n"); // printf("cannot listen\n");
exit(0); // exit(0);
} // }
len = sizeof(cli); // len = sizeof(cli);
connfd = accept(sockfd, (struct sockaddr *)&cli, &len); // connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
if (connfd < 0) { // if (connfd < 0) {
printf("server acccept failed...\n"); // printf("server acccept failed...\n");
exit(0); // exit(0);
} // }
else // else
printf("server acccept the client...\n"); // printf("server acccept the client...\n");
} }