UDP echo example

This commit is contained in:
Kolan Sh 2011-05-04 15:30:24 +04:00
parent 190ee94da5
commit a8bb01e4b5
6 changed files with 237 additions and 0 deletions

View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h> /* для вызова close() для сокета */
int main(void)
{
int sock;
struct sockaddr_in sa;
int bytes_sent;
const char *buffer = "Привет!";
int buffer_length;
buffer_length = strlen(buffer) + 1;
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == -1) {
printf("Ошибка создания сокета");
return 0;
}
sa.sin_family = PF_INET;
sa.sin_addr.s_addr = htonl(0x7F000001);
sa.sin_port = htons(7654);
bytes_sent = sendto(sock, buffer, strlen(buffer) + 1, 0, (struct sockaddr *)&sa, sizeof(struct sockaddr_in));
if (bytes_sent < 0)
printf("Ошибка отправки пакета: %s\n", strerror(errno));
close(sock);
return 0;
}

View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
int main()
{
int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in sa;
int bound;
ssize_t recsize;
socklen_t *address_len = NULL;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons(7654);
bound = bind(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr));
if (bound < 0)
fprintf(stderr, "bind(): ошибка %s\n", strerror(errno));
char Hz[1024];
while (1) {
printf("recv test....\n");
recsize = recvfrom(sock, (void *)Hz, 100, 0, (struct sockaddr *)&sa, address_len);
if (recsize < 0)
perror("Ошибка:");
printf("recsize: %d\n ", (int)recsize);
//sleep(1);
printf("datagram: %s\n", Hz);
}
}

View File

@ -0,0 +1,35 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
static const char *int2ip(unsigned int ip)
{
static char s[16];
sprintf(s, "%d.%d.%d.%d", ip >> 24, ip << 8 >> 24, ip << 16 >> 24, ip << 24 >> 24);
return s;
}
int main(int argc, char *argv[])
{
if (argc < 2) {
perror("need hex ip address");
exit(1);
}
unsigned int ip = 0;
sscanf(argv[1], "%X", &ip);
printf("%s\n", int2ip(ip));
return 0;
}

View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
unsigned int ip2int(const char *s)
{
int ip[4];
sscanf(s, "%d.%d.%d.%d", ip, ip + 1, ip + 2, ip + 3);
return ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3];
}
int main(int argc, char *argv[])
{
if (argc < 2) {
perror("need ip address string");
exit(1);
}
printf("%X\n", ip2int(argv[1]));
return 0;
}

65
c/echo_server-udp/sock1.c Normal file
View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
//~ domain = AF_UNIX, AF_INET, AF_INET6, AF_IPX
//~ type = SOCK_STREAM, SOCK_DGRAM, SOCK_RAW
//~ int socket(int domain, int type, int protocol);
//~ struct sockaddr {
//~ unsigned short sa_family; // Семейство адресов, AF_xxx
//~ char sa_data[14]; // 14 байтов для хранения адреса
//~ };
//~ struct sockaddr_in {
//~ short int sin_family; // Семейство адресов
//~ unsigned short int sin_port; // Номер порта
//~ struct in_addr sin_addr; // IP-адрес
//~ unsigned char sin_zero[8]; // "Дополнение" до размера структуры sockaddr
//~ };
//~ struct in_addr {
//~ unsigned long s_addr;
//~ };
//~ htons, htonl, ntohs, ntohl
//~ int bind(int sockfd, struct sockaddr *addr, int addrlen);
//~ int listen(int sockfd, int backlog);
//~ int accept(int sockfd, void *addr, int *addrlen);
// клиент
//~ int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
//~ int send(int sockfd, const void *msg, int len, int flags);
// отправка буфера целиком
//~ int sendall(int s, char *buf, int len, int flags)
//~ {
//~ int total = 0;
//~ int n;
//~
//~ while(total < len)
//~ {
//~ n = send(s, buf+total, len-total, flags);
//~ if(n == -1) { break; }
//~ total += n;
//~ }
//~
//~ return (n==-1 ? -1 : total);
//~ }
//~ int recv(int sockfd, void *buf, int len, int flags);
// закрытие соединения
//~ int close(int fd);
// запрет передачи 0 - чтения, 1 - записи, 2 - и того, и др.
//~ int shutdown(int sockfd, int how);
int main(int argc, char *argv[])
{
struct a {
char aaa[256];
};
printf("%lu\n", sizeof(struct a));
return 0;
}

25
c/echo_server-udp/tmp1.c Normal file
View File

@ -0,0 +1,25 @@
#include <stdio.h>
void f()
{
int c = 0;
printf("c=%lu\n", (unsigned long)&c);
}
int main(int argc, char *argv[])
{
//~ printf("%lu\n", sizeof(long));
int a = 0;
int pid = fork();
printf("pid=%d\n", pid);
printf("a=%lu\n", (unsigned long)&a);
int b = 0;
printf("b=%lu\n", (unsigned long)&b);
f();
return 0;
}