License rust and c source files

This commit is contained in:
Jianfeng Jiang
2024-01-03 03:22:36 +00:00
committed by Tate, Hongliang Tian
parent 5fb8a9f7e5
commit faaa4438d6
559 changed files with 1192 additions and 89 deletions

View File

@ -1,51 +1,44 @@
// From: https://www.geeksforgeeks.org/socket-programming-cc/.
// Some minor modifications are made to the original code base.
// Lisenced under CCBY-SA.
// SPDX-License-Identifier: MPL-2.0
// Client side C/C++ program to demonstrate socket programming
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
int main(int argc, char const* argv[])
{
int status, valread, client_fd;
int main() {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char* hello = "Hello from client";
char buffer[1024] = { 0 };
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
char *hello = "Hello from client";
char buffer[1024] = {0};
// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary
// form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)
<= 0) {
printf(
"\nInvalid address/ Address not supported \n");
// Convert IPv4 address from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &(serv_addr.sin_addr)) <= 0) {
printf("\n Invalid address/ Address not supported \n");
return -1;
}
if ((status
= connect(client_fd, (struct sockaddr*)&serv_addr,
sizeof(serv_addr)))
< 0) {
printf("\nConnection Failed \n");
// Connect to the server
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\n Connection Failed \n");
return -1;
}
send(client_fd, hello, strlen(hello), 0);
// Send message to the server and receive the reply
send(sock, hello, strlen(hello), 0);
printf("Hello message sent\n");
valread = read(client_fd, buffer, 1024);
printf("%s\n", buffer);
// closing the connected socket
close(client_fd);
valread = read(sock, buffer, 1024);
printf("Server: %s\n", buffer);
return 0;
}
}