~ubuntu-branches/debian/sid/lammps/sid

« back to all changes in this revision

Viewing changes to tools/i-pi/drivers/sockets.c

  • Committer: Package Import Robot
  • Author(s): Anton Gladky
  • Date: 2015-04-29 23:44:49 UTC
  • mfrom: (5.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20150429234449-mbhy9utku6hp6oq8
Tags: 0~20150313.gitfa668e1-1
Upload into unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* A minimal wrapper for socket communication.
 
2
 
 
3
Copyright (C) 2013, Joshua More and Michele Ceriotti
 
4
 
 
5
Permission is hereby granted, free of charge, to any person obtaining
 
6
a copy of this software and associated documentation files (the
 
7
"Software"), to deal in the Software without restriction, including
 
8
without limitation the rights to use, copy, modify, merge, publish,
 
9
distribute, sublicense, and/or sell copies of the Software, and to
 
10
permit persons to whom the Software is furnished to do so, subject to
 
11
the following conditions:
 
12
 
 
13
The above copyright notice and this permission notice shall be included
 
14
in all copies or substantial portions of the Software.
 
15
 
 
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
18
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 
19
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 
20
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 
21
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 
22
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
23
 
 
24
 
 
25
Contains both the functions that transmit data to the socket and read the data
 
26
back out again once finished, and the function which opens the socket initially.
 
27
Can be linked to a FORTRAN code that does not support sockets natively.
 
28
 
 
29
Functions:
 
30
   error: Prints an error message and then exits.
 
31
   open_socket_: Opens a socket with the required host server, socket type and
 
32
      port number.
 
33
   write_buffer_: Writes a string to the socket.
 
34
   read_buffer_: Reads data from the socket.
 
35
*/
 
36
 
 
37
#include <stdio.h>
 
38
#include <stdlib.h>
 
39
#include <unistd.h>
 
40
#include <string.h>
 
41
#include <sys/types.h>
 
42
#include <sys/socket.h>
 
43
#include <netinet/in.h>
 
44
#include <sys/un.h>
 
45
#include <netdb.h>
 
46
 
 
47
void error(const char *msg)
 
48
// Prints an error message and then exits.
 
49
{   perror(msg);  exit(-1);   }
 
50
 
 
51
void open_socket_(int *psockfd, int* inet, int* port, char* host)
 
52
/* Opens a socket.
 
53
 
 
54
Note that fortran passes an extra argument for the string length, but this is
 
55
ignored here for C compatibility.
 
56
 
 
57
Args:
 
58
   psockfd: The id of the socket that will be created.
 
59
   inet: An integer that determines whether the socket will be an inet or unix
 
60
      domain socket. Gives unix if 0, inet otherwise.
 
61
   port: The port number for the socket to be created. Low numbers are often
 
62
      reserved for important channels, so use of numbers of 4 or more digits is
 
63
      recommended.
 
64
   host: The name of the host server.
 
65
*/
 
66
 
 
67
{
 
68
   int sockfd, portno, n;
 
69
   struct hostent *server;
 
70
 
 
71
   struct sockaddr * psock; int ssock;
 
72
 
 
73
   if (*inet>0)
 
74
   {  // creates an internet socket
 
75
      struct sockaddr_in serv_addr;      psock=(struct sockaddr *)&serv_addr;     ssock=sizeof(serv_addr);
 
76
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
 
77
      if (sockfd < 0)  error("Error opening socket");
 
78
 
 
79
      server = gethostbyname(host);
 
80
      if (server == NULL)
 
81
      {
 
82
         fprintf(stderr, "Error opening socket: no such host %s \n", host);
 
83
         exit(-1);
 
84
      }
 
85
 
 
86
      bzero((char *) &serv_addr, sizeof(serv_addr));
 
87
      serv_addr.sin_family = AF_INET;
 
88
      bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
 
89
      serv_addr.sin_port = htons(*port);
 
90
      if (connect(sockfd, psock, ssock) < 0) error("Error opening socket: wrong host address, or broken connection");
 
91
   }
 
92
   else
 
93
   {  // creates a unix socket
 
94
      struct sockaddr_un serv_addr;      psock=(struct sockaddr *)&serv_addr;     ssock=sizeof(serv_addr);
 
95
      sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
 
96
      bzero((char *) &serv_addr, sizeof(serv_addr));
 
97
      serv_addr.sun_family = AF_UNIX;
 
98
      strcpy(serv_addr.sun_path, "/tmp/ipi_");
 
99
      strcpy(serv_addr.sun_path+9, host);
 
100
      if (connect(sockfd, psock, ssock) < 0) error("Error opening socket: wrong host address, or broken connection");
 
101
   }
 
102
 
 
103
   *psockfd=sockfd;
 
104
}
 
105
 
 
106
void writebuffer_(int *psockfd, char *data, int* plen)
 
107
/* Writes to a socket.
 
108
 
 
109
Args:
 
110
   psockfd: The id of the socket that will be written to.
 
111
   data: The data to be written to the socket.
 
112
   plen: The length of the data in bytes.
 
113
*/
 
114
 
 
115
{
 
116
   int n;
 
117
   int sockfd=*psockfd;
 
118
   int len=*plen;
 
119
 
 
120
   n = write(sockfd,data,len);
 
121
   if (n < 0) error("Error writing to socket: server has quit or connection broke");
 
122
}
 
123
 
 
124
 
 
125
void readbuffer_(int *psockfd, char *data, int* plen)
 
126
/* Reads from a socket.
 
127
 
 
128
Args:
 
129
   psockfd: The id of the socket that will be read from.
 
130
   data: The storage array for data read from the socket.
 
131
   plen: The length of the data in bytes.
 
132
*/
 
133
 
 
134
{
 
135
   int n, nr;
 
136
   int sockfd=*psockfd;
 
137
   int len=*plen;
 
138
 
 
139
   n = nr = read(sockfd,data,len);
 
140
 
 
141
   while (nr>0 && n<len )
 
142
   {  nr=read(sockfd,&data[n],len-n); n+=nr; }
 
143
 
 
144
   if (n == 0) error("Error reading from socket: server has quit or connection broke");
 
145
}
 
146
 
 
147