1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | #include <sys/socket.h> |
---|
6 | #include <sys/types.h> |
---|
7 | |
---|
8 | #include <netinet/in.h> |
---|
9 | #include <netdb.h> |
---|
10 | #include <unistd.h> |
---|
11 | #include <fcntl.h> |
---|
12 | |
---|
13 | #define IP "localhost" |
---|
14 | |
---|
15 | #define PORT "8888" |
---|
16 | |
---|
17 | int main(void) |
---|
18 | { |
---|
19 | struct sockaddr_in socketaddr; |
---|
20 | int socketfd, |
---|
21 | nHostPort; |
---|
22 | int err, |
---|
23 | i, |
---|
24 | unit; |
---|
25 | char buff[4096]; |
---|
26 | char * _xml = (char)NULL, |
---|
27 | * xml_connect = {"<?xml version='1.0' encoding='UTF-8'?><stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>"}; |
---|
28 | |
---|
29 | _xml = (char *)malloc(sizeof(xml_connect)); |
---|
30 | |
---|
31 | if ( (nHostPort = strtol(PORT, 0, 10)) == 0 ) |
---|
32 | { |
---|
33 | printf("\n<port>\n\n"); |
---|
34 | printf("\ncould not make a socket\n"); |
---|
35 | return 0; |
---|
36 | } |
---|
37 | |
---|
38 | socketfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
39 | |
---|
40 | socketaddr.sin_family = AF_INET; |
---|
41 | socketaddr.sin_addr.s_addr = INADDR_ANY;//inet_addr(IP); |
---|
42 | socketaddr.sin_port = htons(nHostPort); |
---|
43 | |
---|
44 | //printf("::: %d\n\n\n", fcntl( socketfd, F_SETFL, fcntl( socketfd, F_GETFL ) & ~O_NONBLOCK )); |
---|
45 | //printf("::: %d\n\n\n", fcntl( socketfd, F_SETFL, fcntl( socketfd, F_GETFL ) & O_NONBLOCK )); |
---|
46 | |
---|
47 | |
---|
48 | err = connect(socketfd, (struct sockaddr*)&socketaddr, sizeof(socketaddr)); |
---|
49 | |
---|
50 | if ( err < 0 ) |
---|
51 | { |
---|
52 | printf("Socket returned error #%d,program terminated\n", err); |
---|
53 | exit(0); |
---|
54 | } |
---|
55 | |
---|
56 | sprintf(_xml, xml_connect, "localhost"); |
---|
57 | |
---|
58 | write(socketfd, _xml, strlen(_xml)); |
---|
59 | |
---|
60 | read (socketfd, buff, sizeof(buff)); |
---|
61 | printf("%s\n\n", buff); |
---|
62 | exit(0); |
---|
63 | } |
---|