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
// listen
// send
// recv
// bind
// strstr
// malloc
// realloc
// free
// calloc
// bzero
// atoi
// sprintf
// strlen
// bind
// bzero
// calloc
// close
// exit
// strcpy
// strcat
// free
// listen
// malloc
// memset
// realloc
// recv
// select
// send
// socket
// sprintf
// strcat
// strcpy
// strlen
// strstr
// write
int main() {
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
servaddr.sin_port = htons(8081);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) {
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");
void putstr(int fd, char *str) {
write(fd, str, strlen(str));
}
void error(char *str, int code) {
putstr(2, str);
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
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
error("socket creation failed...\n", 0);
else
putstr(1, "Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(2130706433); //127.0.0.1
servaddr.sin_port = htons(8081);
// // Binding newly created socket to given IP and verification
// if ((bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) {
// 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");
}