CでHello World

(20)
http://yamada.daiji.ro/blog/?p=1044

apt install build-essential

gcc --version

vim hello.c

#include <stdio.h>

int main(void)
{
printf("Hello World!\n");

return 0;
}

gcc -o hello hello.c
./hello

--
vim web.c

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
int sock0;
struct sockaddr_in addr;
struct sockaddr_in client;
int len;
int sock;
int yes = 1;

char buf[2048];
char inbuf[2048];

sock0 = socket(AF_INET, SOCK_STREAM, 0);

addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = INADDR_ANY;
setsockopt(sock0, SOL_SOCKET, SO_REUSEADDR, (const char *) &yes, sizeof(yes));
bind(sock0, (struct sockaddr *) &addr, sizeof(addr));
listen(sock0, 5);

memset(buf, 0, sizeof(buf));
snprintf(buf, sizeof(buf),
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"Hello World!!\r\n");
while (1) {
len = sizeof(client);
sock = accept(sock0, (struct sockaddr *) &client, &len);
memset(inbuf, 0, sizeof(inbuf));
recv(sock, inbuf, sizeof(inbuf), 0);
send(sock, buf, (int) strlen(buf), 0);
close(sock);
}
close(sock0);
return 0;
}


gcc -o web web.c
./web

 

(10)

apt install build-essential

gcc --version

vim hello.c

#include <stdio.h>

int main(void)
{
printf("Hello World!\n");

return 0;
}

gcc -o hello hello.c
./hello

--
vim web.c

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
int sock0;
struct sockaddr_in addr;
struct sockaddr_in client;
int len;
int sock;
int yes = 1;

char buf[2048];
char inbuf[2048];

sock0 = socket(AF_INET, SOCK_STREAM, 0);

addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = INADDR_ANY;
setsockopt(sock0, SOL_SOCKET, SO_REUSEADDR, (const char *) &yes, sizeof(yes));
bind(sock0, (struct sockaddr *) &addr, sizeof(addr));
listen(sock0, 5);

memset(buf, 0, sizeof(buf));
snprintf(buf, sizeof(buf),
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"Hello World!!\r\n");
while (1) {
len = sizeof(client);
sock = accept(sock0, (struct sockaddr *) &client, &len);
memset(inbuf, 0, sizeof(inbuf));
recv(sock, inbuf, sizeof(inbuf), 0);
send(sock, buf, (int) strlen(buf), 0);
close(sock);
}
close(sock0);
return 0;
}


gcc -o web web.c
./web

 

 

(8)

gcc --version

vim hello.c

#include <stdio.h>

int main(void)
{
printf("Hello World!\n");

return 0;
}

gcc -o hello hello.c
./hello

--
vim web.c

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
int sock0;
struct sockaddr_in addr;
struct sockaddr_in client;
int len;
int sock;
int yes = 1;

char buf[2048];
char inbuf[2048];

sock0 = socket(AF_INET, SOCK_STREAM, 0);

addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = INADDR_ANY;
setsockopt(sock0, SOL_SOCKET, SO_REUSEADDR, (const char *) &yes, sizeof(yes));
bind(sock0, (struct sockaddr *) &addr, sizeof(addr));
listen(sock0, 5);

memset(buf, 0, sizeof(buf));
snprintf(buf, sizeof(buf),
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"Hello World!!\r\n");
while (1) {
len = sizeof(client);
sock = accept(sock0, (struct sockaddr *) &client, &len);
memset(inbuf, 0, sizeof(inbuf));
recv(sock, inbuf, sizeof(inbuf), 0);
send(sock, buf, (int) strlen(buf), 0);
close(sock);
}
close(sock0);
return 0;
}


gcc -o web web.c
./web

 

(2019)

https://hacknote.jp/archives/53289/
https://www.geekpage.jp/programming/winsock/http-server.php

cd C:\MinGW\bin

gcc --version

notepad hello.c

#include <stdio.h>

int main(void)
{
printf("Hello World!\n");

return 0;
}

gcc -o hello hello.c
hello

--
notepad web.c

#include <stdio.h>
#include <winsock2.h>

int
main()
{
WSADATA wsaData;
SOCKET sock0;
struct sockaddr_in addr;
struct sockaddr_in client;
int len;
SOCKET sock;
BOOL yes = 1;

char buf[2048];
char inbuf[2048];

WSAStartup(MAKEWORD(2,0), &wsaData);

sock0 = socket(AF_INET, SOCK_STREAM, 0);

addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.S_un.S_addr = INADDR_ANY;
setsockopt(sock0, SOL_SOCKET, SO_REUSEADDR, (const char *)&yes, sizeof(yes));
bind(sock0, (struct sockaddr *)&addr, sizeof(addr));
listen(sock0, 5);

memset(buf, 0, sizeof(buf));
_snprintf(buf, sizeof(buf),
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"Hello World!!\r\n");
while (1) {
len = sizeof(client);
sock = accept(sock0, (struct sockaddr *)&client, &len);
memset(inbuf, 0, sizeof(inbuf));
recv(sock, inbuf, sizeof(inbuf), 0);
send(sock, buf, (int)strlen(buf), 0);
closesocket(sock);
}

WSACleanup();

return 0;
}

gcc -o web web.c -lwsock32 -lws2_32
web