mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-21 00:06:34 +00:00
Use docker as dev environment
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
bf961756b9
commit
9137ef434f
51
regression/apps/network/tcp_client.c
Normal file
51
regression/apps/network/tcp_client.c
Normal file
@ -0,0 +1,51 @@
|
||||
// From: https://www.geeksforgeeks.org/socket-programming-cc/.
|
||||
// Some minor modifications are made to the original code base.
|
||||
// Lisenced under CCBY-SA.
|
||||
|
||||
// Client side C/C++ program to demonstrate socket programming
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#define PORT 8080
|
||||
|
||||
int main(int argc, char const* argv[])
|
||||
{
|
||||
int status, valread, client_fd;
|
||||
struct sockaddr_in serv_addr;
|
||||
char* hello = "Hello from client";
|
||||
char buffer[1024] = { 0 };
|
||||
if ((client_fd = 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");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((status
|
||||
= connect(client_fd, (struct sockaddr*)&serv_addr,
|
||||
sizeof(serv_addr)))
|
||||
< 0) {
|
||||
printf("\nConnection Failed \n");
|
||||
return -1;
|
||||
}
|
||||
send(client_fd, 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);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user