NAMD
Functions
vmdsock.h File Reference

Go to the source code of this file.

Functions

int vmdsock_init (void)
 
voidvmdsock_create (void)
 
int vmdsock_bind (void *, int)
 
int vmdsock_listen (void *)
 
voidvmdsock_accept (void *)
 
int vmdsock_connect (void *, const char *, int)
 
int vmdsock_write (void *, const void *, int)
 
int vmdsock_read (void *, void *, int)
 
int vmdsock_selread (void *, int)
 
int vmdsock_selwrite (void *, int)
 
void vmdsock_destroy (void *)
 

Function Documentation

void* vmdsock_accept ( void )

Definition at line 128 of file vmdsock.C.

Referenced by GlobalMasterIMD::calculate().

128  {
129  int rc;
130  vmdsocket *new_s = NULL, *s = (vmdsocket *) v;
131  socklen_t len;
132 
133  len = sizeof(s->addr);
134  rc = accept(s->sd, (struct sockaddr *) &s->addr, &len);
135  if (rc >= 0) {
136  new_s = (vmdsocket *) malloc(sizeof(vmdsocket));
137  if (new_s != NULL) {
138  *new_s = *s;
139  new_s->sd = rc;
140  }
141  }
142  return (void *)new_s;
143 }
int vmdsock_bind ( void ,
int   
)

Definition at line 114 of file vmdsock.C.

Referenced by find_free_port().

114  {
115  vmdsocket *s = (vmdsocket *) v;
116  memset(&(s->addr), 0, sizeof(s->addr));
117  s->addr.sin_family = PF_INET;
118  s->addr.sin_port = htons(port);
119 
120  return bind(s->sd, (struct sockaddr *) &s->addr, sizeof(s->addr));
121 }
int vmdsock_connect ( void ,
const char *  ,
int   
)

Definition at line 92 of file vmdsock.C.

92  {
93  vmdsocket *s = (vmdsocket *) v;
94  char address[1030];
95  struct hostent *h;
96 
97  h=gethostbyname(host);
98  if (h == NULL)
99  return -1;
100  sprintf(address, "%d.%d.%d.%d",
101  (unsigned char) h->h_addr_list[0][0],
102  (unsigned char) h->h_addr_list[0][1],
103  (unsigned char) h->h_addr_list[0][2],
104  (unsigned char) h->h_addr_list[0][3]);
105 
106  memset(&(s->addr), 0, sizeof(s->addr));
107  s->addr.sin_family = PF_INET;
108  s->addr.sin_addr.s_addr = inet_addr(address);
109  s->addr.sin_port = htons(port);
110 
111  return connect(s->sd, (struct sockaddr *) &s->addr, sizeof(s->addr));
112 }
void* vmdsock_create ( void  )

Definition at line 76 of file vmdsock.C.

Referenced by GlobalMasterIMD::GlobalMasterIMD().

76  {
77  vmdsocket * s;
78 
79  s = (vmdsocket *) malloc(sizeof(vmdsocket));
80  if (s != NULL)
81  memset(s, 0, sizeof(vmdsocket));
82 
83  if ((s->sd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
84  printf("Failed to open socket.");
85  free(s);
86  return NULL;
87  }
88 
89  return (void *) s;
90 }
void vmdsock_destroy ( void )

Definition at line 164 of file vmdsock.C.

Referenced by GlobalMasterIMD::calculate(), GlobalMasterIMD::get_vmd_forces(), GlobalMasterIMD::GlobalMasterIMD(), and GlobalMasterIMD::~GlobalMasterIMD().

164  {
165  vmdsocket * s = (vmdsocket *) v;
166  if (s == NULL)
167  return;
168 
169 #if defined(WIN32) && !defined(__CYGWIN__)
170  closesocket(s->sd);
171 #else
172  close(s->sd);
173 #endif
174  free(s);
175 }
int vmdsock_init ( void  )

Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by The Board of Trustees of the University of Illinois. All rights reserved.

Definition at line 52 of file vmdsock.C.

Referenced by GlobalMasterIMD::GlobalMasterIMD().

52  {
53 #if defined(WIN32) && !defined(__CYGWIN__)
54  int rc = 0;
55 
56 /*
57 ** XXX static and global variables are unsafe for shared memory builds.
58 ** The global and static vars should be eliminated.
59 */
60  static int initialized=0;
61 
62  if (!initialized) {
63  WSADATA wsdata;
64  rc = WSAStartup(MAKEWORD(1,1), &wsdata);
65  if (rc == 0)
66  initialized = 1;
67  }
68 
69  return rc;
70 #else
71  return 0;
72 #endif
73 }
int vmdsock_listen ( void )

Definition at line 123 of file vmdsock.C.

Referenced by GlobalMasterIMD::GlobalMasterIMD().

123  {
124  vmdsocket *s = (vmdsocket *) v;
125  return listen(s->sd, 5);
126 }
int vmdsock_read ( void ,
void ,
int   
)

Definition at line 154 of file vmdsock.C.

Referenced by imd_readn().

154  {
155  vmdsocket *s = (vmdsocket *) v;
156 #if defined(WIN32) && !defined(__CYGWIN__)
157  return recv(s->sd, (char*) buf, len, 0); // windows lacks the read() call
158 #else
159  return read(s->sd, buf, len);
160 #endif
161 
162 }
int vmdsock_selread ( void ,
int   
)

Definition at line 177 of file vmdsock.C.

Referenced by GlobalMasterIMD::calculate(), GlobalMasterIMD::get_vmd_forces(), imd_recv_handshake(), and my_imd_connect().

177  {
178  vmdsocket *s = (vmdsocket *)v;
179  fd_set rfd;
180  struct timeval tv;
181  int rc;
182 
183  FD_ZERO(&rfd);
184  FD_SET(s->sd, &rfd);
185  memset((void *)&tv, 0, sizeof(struct timeval));
186  tv.tv_sec = sec;
187  do {
188  rc = select(s->sd+1, &rfd, NULL, NULL, &tv);
189  } while (rc < 0 && errno == EINTR);
190  return rc;
191 
192 }
int vmdsock_selwrite ( void ,
int   
)

Definition at line 194 of file vmdsock.C.

Referenced by GlobalMasterIMD::send_energies(), and GlobalMasterIMD::send_fcoords().

194  {
195  vmdsocket *s = (vmdsocket *)v;
196  fd_set wfd;
197  struct timeval tv;
198  int rc;
199 
200  FD_ZERO(&wfd);
201  FD_SET(s->sd, &wfd);
202  memset((void *)&tv, 0, sizeof(struct timeval));
203  tv.tv_sec = sec;
204  do {
205  rc = select(s->sd + 1, NULL, &wfd, NULL, &tv);
206  } while (rc < 0 && errno == EINTR);
207  return rc;
208 }
int vmdsock_write ( void ,
const void ,
int   
)

Definition at line 145 of file vmdsock.C.

Referenced by imd_writen().

145  {
146  vmdsocket *s = (vmdsocket *) v;
147 #if defined(WIN32) && !defined(__CYGWIN__)
148  return send(s->sd, (const char*) buf, len, 0); // windows lacks the write() call
149 #else
150  return write(s->sd, buf, len);
151 #endif
152 }