Unix/Linux socket wypros
9 отговора
361 прегледа
Unix/Linux socket wypros
Wyzmojno li e pod Linux pri rabota sys soketi da se izpolzwat write i read wmesto
send i recv? 4uwal sym 4e pod unix-podobnite OS wsi4ko e failowe ama dali send/recv sa wzaimno zamenqmemi sys write/read? Blagodarq predwaritelno.
send i recv? 4uwal sym 4e pod unix-podobnite OS wsi4ko e failowe ama dali send/recv sa wzaimno zamenqmemi sys write/read? Blagodarq predwaritelno.
Re: redundant
То па е адски трудно да си направиш имплементацията сам.
Я вышел им наперерез...
Я вышел им наперерез...
moje moje
[линк]
poopravq malko win-a, ne che neshto moje da go opravi, ama pone moje da svyrhish tui onui i pod win
IF JADEN THEN
GOTO BIRARIA;
poopravq malko win-a, ne che neshto moje da go opravi, ama pone moje da svyrhish tui onui i pod win
IF JADEN THEN
GOTO BIRARIA;
sto nqkoj ne postne malak sample
interesno mi e kak se pravi
sto nqkoj ne postne malak sample
kpym
sto nqkoj ne postne malak sample
kpym
Re: sto nqkoj ne postne malak sample
Hello World server
usage: telnet host 6666
usage: telnet host 6666
/* hello-world-server.c - a "hello world" server */
#include <stdio.h> /* Basic I/O routines */
#include <sys/types.h> /* standard system types */
#include <netinet/in.h> /* Internet address structures */
#include <sys/socket.h> /* socket interface functions */
#include <netdb.h> /* host to IP resolution */
#define PORT 6666 /* port of "hello world" server */
#define LINE "hello world\n" /* what to say to our clients */
void main()
{
int rc; /* system calls return value storage */
int s; /* socket descriptor */
int cs; /* new connection's socket descriptor */
struct sockaddr_in sa; /* Internet address struct */
struct sockaddr_in csa; /* client's address struct */
int size_csa; /* size of client's address struct */
char msg[255];
/* initiate machine's Internet address structure */
/* first clear out the struct, to avoid garbage */
memset(&sa, 0, sizeof(sa));
/* Using Internet address family */
sa.sin_family = AF_INET;
/* copy port number in network byte order */
sa.sin_port = htons(PORT);
/* we will accept cnnections coming through any IP */
/* address that belongs to our host, using the */
/* INADDR_ANY wild-card. */
/* allocate a free socket */
/* Internet address family, Stream socket */
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("socket: allocation failed");
}
/* bind the socket to the newly formed address */
rc = bind(s, (struct sockaddr *)&sa, sizeof(sa));
/* check there was no error */
if (rc) {
perror("bind");
exit(1);
}
/* ask the system to listen for incoming connections */
/* to the address we just bound. specify that up to */
/* 5 pending connection requests will be queued by the */
/* system, if we are not directly awaiting them using */
/* the accept() system cal, when they arrive. */
rc = listen(s, 5);
/* check there was no error */
if (rc) {
perror("listen");
}
/* remember size for later usage */
size_csa = sizeof(csa);
/* enter an accept-write-close infinite loop */
while (1) {
/* the accept() system call will wait for a */
/* connection, and when one is established, a */
/* new socket will be created to form it, and */
/* the csa variable will hold the address */
/* of the Client that just connected to us. */
/* the old socket, s, will still be available */
/* for future accept() statements. */
cs = accept(s, (struct sockaddr *)&csa, &size_csa);
/* check for errors. if any, enter accept mode again */
if (cs < 0)
continue;
/* ok, we got a new connection. do the job... */
write(cs, LINE, sizeof(LINE));
/* now close the connection */
close(cs);
}
}