「🏗️」 wip: wtf is going on
This commit is contained in:
96
subjects/main.c
Normal file
96
subjects/main.c
Normal file
@ -0,0 +1,96 @@
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
int extract_message(char **buf, char **msg)
|
||||
{
|
||||
char *newbuf;
|
||||
int i;
|
||||
|
||||
*msg = 0;
|
||||
if (*buf == 0)
|
||||
return (0);
|
||||
i = 0;
|
||||
while ((*buf)[i])
|
||||
{
|
||||
if ((*buf)[i] == '\n')
|
||||
{
|
||||
newbuf = calloc(1, sizeof(*newbuf) * (strlen(*buf + i + 1) + 1));
|
||||
if (newbuf == 0)
|
||||
return (-1);
|
||||
strcpy(newbuf, *buf + i + 1);
|
||||
*msg = *buf;
|
||||
(*msg)[i + 1] = 0;
|
||||
*buf = newbuf;
|
||||
return (1);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *str_join(char *buf, char *add)
|
||||
{
|
||||
char *newbuf;
|
||||
int len;
|
||||
|
||||
if (buf == 0)
|
||||
len = 0;
|
||||
else
|
||||
len = strlen(buf);
|
||||
newbuf = malloc(sizeof(*newbuf) * (len + strlen(add) + 1));
|
||||
if (newbuf == 0)
|
||||
return (0);
|
||||
newbuf[0] = 0;
|
||||
if (buf != 0)
|
||||
strcat(newbuf, buf);
|
||||
free(buf);
|
||||
strcat(newbuf, add);
|
||||
return (newbuf);
|
||||
}
|
||||
|
||||
|
||||
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");
|
||||
}
|
Reference in New Issue
Block a user