Sockets: Difference between revisions

1,126 bytes added ,  15 years ago
added C
(added haskell; not tested)
(added C)
Line 26:
end;
</ada>
 
=={{header|C}}==
{{works with|POSIX|.1-2001}}
{{works with|gcc|4.2.2}}
 
With little changes it could work on MS Windows (without Cygwin) too. But I don't know exactly how. I have tested it using the <tt>netcat -l -p 256</tt>.
 
<c>#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
 
char msg[] = "hello socket world";
 
int main()
{
int r, i, sock, len, slen;
char *pm = msg;
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
r = getaddrinfo("localhost", "256", &hints, &res);
if ( r == 0 )
{
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if ( sock >= 0 )
{
if ( connect(sock, res->ai_addr, res->ai_addrlen) >= 0 )
{
do
{
len = strlen(pm);
slen = send(sock, pm, len, 0);
pm += slen;
} while ( slen < len );
}
close(sock);
}
freeaddrinfo(res);
}
}</c>
 
=={{header|D}}==