sock_inet
This commit is contained in:
parent
b3763ff62c
commit
7012521d08
5 changed files with 245 additions and 0 deletions
19
socket_inet/Makefile
Normal file
19
socket_inet/Makefile
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
CC ?= gcc
|
||||||
|
CFLAGS ?= -Wall -g
|
||||||
|
|
||||||
|
all: server.elf client.elf
|
||||||
|
|
||||||
|
server.elf: server.c
|
||||||
|
client.elf: client.c
|
||||||
|
|
||||||
|
%.elf: %.c
|
||||||
|
@echo CC $@
|
||||||
|
@$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
|
||||||
|
|
||||||
|
test: all
|
||||||
|
bash test.sh
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.elf *.o
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
54
socket_inet/client.c
Normal file
54
socket_inet/client.c
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define MAX 80
|
||||||
|
#define PORT 8080
|
||||||
|
|
||||||
|
void func(int sockfd) {
|
||||||
|
char buff[MAX];
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
char *msg = "Ping\n";
|
||||||
|
write(sockfd, msg, sizeof(buff));
|
||||||
|
|
||||||
|
bzero(buff, sizeof(buff));
|
||||||
|
read(sockfd, buff, sizeof(buff));
|
||||||
|
|
||||||
|
printf("From Server : %s", buff);
|
||||||
|
if ((strncmp(buff, "exit", 4)) == 0) {
|
||||||
|
printf("Client Exit...\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (sockfd == -1) {
|
||||||
|
printf("socket creation failed...\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
printf("Socket successfully created..\n");
|
||||||
|
|
||||||
|
struct sockaddr_in servaddr;
|
||||||
|
bzero(&servaddr, sizeof(servaddr));
|
||||||
|
servaddr.sin_family = AF_INET;
|
||||||
|
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||||
|
servaddr.sin_port = htons(PORT);
|
||||||
|
|
||||||
|
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
|
||||||
|
printf("connection with the server failed...\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
func(sockfd);
|
||||||
|
close(sockfd);
|
||||||
|
}
|
||||||
73
socket_inet/server.c
Normal file
73
socket_inet/server.c
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define MAX 80
|
||||||
|
#define PORT 8080
|
||||||
|
|
||||||
|
void func(int connfd) {
|
||||||
|
char buff[MAX];
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
bzero(buff, MAX);
|
||||||
|
|
||||||
|
ssize_t recv_n = recv(connfd, buff, sizeof(buff), 0);
|
||||||
|
if (recv_n >= MAX) {
|
||||||
|
/* Likely more data, realloc */
|
||||||
|
}
|
||||||
|
|
||||||
|
buff[recv_n - 1] = '\0';
|
||||||
|
printf("From client: %s\n", buff);
|
||||||
|
bzero(buff, MAX);
|
||||||
|
|
||||||
|
char *msg = "Hello!\n";
|
||||||
|
send(connfd, msg, strlen(msg), 0);
|
||||||
|
|
||||||
|
if (strncmp("exit", buff, 4) == 0) {
|
||||||
|
printf("Server Exit...\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (sockfd == -1) {
|
||||||
|
printf("socket creation failed...\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_in servaddr;
|
||||||
|
bzero(&servaddr, sizeof(servaddr));
|
||||||
|
|
||||||
|
servaddr.sin_family = AF_INET;
|
||||||
|
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
servaddr.sin_port = htons(PORT);
|
||||||
|
|
||||||
|
if ((bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) {
|
||||||
|
printf("socket bind failed...\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((listen(sockfd, 5)) != 0) {
|
||||||
|
printf("Listen failed...\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_in cli;
|
||||||
|
socklen_t len = sizeof(cli);
|
||||||
|
|
||||||
|
int connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
|
||||||
|
if (connfd < 0) {
|
||||||
|
printf("server accept failed...\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
func(connfd);
|
||||||
|
close(sockfd);
|
||||||
|
}
|
||||||
73
socket_inet/socket.c
Normal file
73
socket_inet/socket.c
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define SOCKET_PATH "/tmp/demosocket"
|
||||||
|
#define BUF_SIZE 128
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int server_fd, client_fd;
|
||||||
|
struct sockaddr_un addr;
|
||||||
|
char buf[BUF_SIZE];
|
||||||
|
|
||||||
|
// Clean up any leftover socket file
|
||||||
|
unlink(SOCKET_PATH);
|
||||||
|
|
||||||
|
// Create socket
|
||||||
|
server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if (server_fd == -1) {
|
||||||
|
perror("socket");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zero out and set up the address structure
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path) - 1);
|
||||||
|
|
||||||
|
// Bind the socket to the path
|
||||||
|
if (bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
|
||||||
|
perror("bind");
|
||||||
|
close(server_fd);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen for a connection
|
||||||
|
if (listen(server_fd, 1) == -1) {
|
||||||
|
perror("listen");
|
||||||
|
close(server_fd);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Server listening on %s...\n", SOCKET_PATH);
|
||||||
|
|
||||||
|
// Accept a connection
|
||||||
|
client_fd = accept(server_fd, NULL, NULL);
|
||||||
|
if (client_fd == -1) {
|
||||||
|
perror("accept");
|
||||||
|
close(server_fd);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Receive a message
|
||||||
|
ssize_t received = recv(client_fd, buf, BUF_SIZE - 1, 0);
|
||||||
|
if (received > 0) {
|
||||||
|
buf[received] = '\0';
|
||||||
|
printf("Received: %s", buf);
|
||||||
|
|
||||||
|
// Send a reply
|
||||||
|
const char *reply = "Got your message!\n";
|
||||||
|
send(client_fd, reply, strlen(reply), 0);
|
||||||
|
} else {
|
||||||
|
perror("recv");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
close(client_fd);
|
||||||
|
close(server_fd);
|
||||||
|
unlink(SOCKET_PATH);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
26
socket_inet/test.sh
Normal file
26
socket_inet/test.sh
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
SOCKET_PATH="/tmp/demosocket"
|
||||||
|
|
||||||
|
# Cleanup function to be run on exit
|
||||||
|
cleanup() {
|
||||||
|
echo "Cleaning up..."
|
||||||
|
if [[ -n "$SERVER_PID" ]]; then
|
||||||
|
kill "$SERVER_PID" 2>/dev/null || true
|
||||||
|
wait "$SERVER_PID" 2>/dev/null || true
|
||||||
|
fi
|
||||||
|
rm -f "$SOCKET_PATH"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Trap EXIT to ensure cleanup runs
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
echo "Starting server..."
|
||||||
|
./server.elf &
|
||||||
|
SERVER_PID=$!
|
||||||
|
|
||||||
|
# Wait briefly for the server to start
|
||||||
|
sleep 0.2
|
||||||
|
|
||||||
|
echo "Running client..."
|
||||||
|
./client.elf
|
||||||
Loading…
Add table
Add a link
Reference in a new issue