-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectToServer.c
More file actions
147 lines (122 loc) · 2.66 KB
/
connectToServer.c
File metadata and controls
147 lines (122 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdlib.h>
#include <stdio.h>
#include "connectToServer.h"
#include "mex.h"
#ifndef WIN32
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#define SOCKET_ERROR -1
#else
int initWinsock()
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1,1);
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{ mexErrMsgTxt("Init Failed"); return 1; }
return 0;
}
#endif
/* Read a line from a socket */
int Readline(int sockd, void *vptr, int maxlen) {
int n, rc;
char c, *buffer;
buffer = vptr;
for ( n = 1; n < maxlen; n++ ) {
if ( (rc = recv(sockd, &c, 1,0)) == 1 ) {
*buffer++ = c;
if ( c == '\n' )
break;
}
else if ( rc == 0 ) {
if ( n == 1 )
return 0;
else
break;
}
else {
return -1;
}
}
*buffer = 0;
return n;
}
/* Write a line to a socket */
int Writeline(int sockd, const void *vptr, int n) {
int nleft;
int nwritten;
const char *buffer;
buffer = vptr;
nleft = n;
while ( nleft > 0 ) {
if ( (nwritten = send(sockd, buffer, nleft,0)) == SOCKET_ERROR ) {
return -1;
}
nleft -= nwritten;
buffer += nwritten;
}
return n;
}
int ConnectTo( char *host, short port ){
int sck;
struct sockaddr_in writer;
struct hostent *hp;
#ifdef WIN32
initWinsock();
#endif
/* get host information */
hp = gethostbyname( host );
if( hp == 0x0 ){
mexErrMsgTxt("Host unknown");
return 0;
}
/* reset writer struct and copy over what we know */
memset( (char *) &writer, 0, sizeof(writer) );
memcpy( (char *) &writer.sin_addr, hp->h_addr, hp->h_length );
writer.sin_family = hp->h_addrtype;
writer.sin_port = htons( (short)port );
/* create socket */
sck = socket( AF_INET, SOCK_STREAM, 0 );
if ( sck < 0 ){
mexErrMsgTxt("Could not open socket.");
return 0;
}
/* Establish connection */
if( connect( sck, (struct sockaddr *) &writer, sizeof(writer) ) < 0 ) {
mexErrMsgTxt("Could not establish connection.");
return 0;
}
return sck;
}
void CloseConnection(int sockd)
{
#ifdef WIN32
closesocket(sockd);
WSACleanup();
#else
close(sockd);
#endif
}
/*
int
main(int argc,char **argv)
{
FILE *sp;
int done=0;
int numRead;
char buf[500];
initWinsock();
sp = ConnectTo("aurora",4765);
printf("opened socket\n");
fprintf(sp,"get contacts\n");
while (!done) {
fgets(buf,500,sp);
printf("%s\n",buf);
if (!strcmp(buf,"done\n")) done = 1;
}
fclose(sp);
return 0;
}
*/