Сокеты в Linux.
Кто работал?
Как можно открыть неблокирующий сокет, или в болкирующем перед чтением из него узнать сколько байт ожидает в нём?
Модератор: Модераторы
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
int
make_named_socket (const char *filename)
{
struct sockaddr_un name;
int sock;
size_t size;
sock = socket (PF_UNIX, SOCK_DGRAM, 0);
if (sock < 0)
{
perror ("socket");
exit (EXIT_FAILURE);
}
name.sun_family = AF_FILE;
strcpy (name.sun_path, filename);
size=(offsetof(struct sockaddr_un, sun_path)
+ strlen (name.sun_path) + 1);
if (bind (sock, (struct sockaddr *) &name,
size) < 0) {
perror ("bind");
exit (EXIT_FAILURE);
}
return sock;
}
procedure mysoc();
var
hp:integer;
sa:sockaddr;
va1:Longword;
su:sockaddr_un;
begin
su.sun_family:=AF_FILE;//AF_UNIX;
//su.sun_path:='/home/x';
hp:=socket(PF_UNIX, SOCK_DGRAM, 0);
sa.sin_family:=su.sun_family;
if hp=-1 then showmessage('Error. Socket not created') else showmessage('Socket id= '+inttostr(hp));
if libc.connect(hp,@su,20)=-1 then showmessage('Error.Not conect') else showmessage('Conect...= '+inttostr(hp));
if getpeername(hp,sa,va1)=-1 then showmessage('Error.Not get peername') else showmessage('Conect... peer name= '+inttostr(hp));
fcntl(hp, F_GETFL);
fcntl(hp, F_SETFL, O_RDWR or O_NONBLOCK);
fcntl(hp, F_SETFD, FD_CLOEXEC);
if shutdown(hp,2)=-1 then showmessage('Error. Socket not closed') else showmessage('Socket closed');
end;
Вернуться в Free Pascal Compiler
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 2