1 /***************************************************************************
2 * Copyright (C) 2005 by Dimitris Saougos & Filippos Papadopoulos *
3 * <psybases@gmail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include <sys/select.h>
32 #include <sys/types.h>
35 Socket::Socket(int sd)
39 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x)) == -1)
46 Socket::Socket(int type, int protocol)
54 s=socket(AF_INET, t, protocol);
55 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x)) == -1)
65 shutdown(s, SHUT_RDWR);
70 int Socket::Bind(int port)
72 struct sockaddr_in myaddr;
74 myaddr.sin_family=AF_INET;
75 myaddr.sin_port=htons(port);
76 myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
78 int res=bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr));
80 perror("bind returned -1"), exit(1);
85 int Socket::Listen(int log)
87 int res=listen(s, log);
89 perror("listen returned -1"), exit(1);
94 Socket* Socket::Accept(void)
96 socklen_t l = sizeof(struct sockaddr);
98 int res = accept(s, (struct sockaddr *)&peer, &l);
100 perror("accept returned -1"), exit(1);
102 Socket *tmp = new Socket((int)res);
108 int Socket::Connect()
111 tmp.s_addr = peer.sin_addr.s_addr;
113 printf(" con to %s %d\n", inet_ntoa(tmp), ntohs(peer.sin_port));
115 res = connect(s, (struct sockaddr *)&peer, sizeof(peer));
118 perror("connect returned -1");
126 int Socket::Send(void *buffer, unsigned long size)
128 // printf("send buf=%s\n", (const char *)buffer);
129 int res=send(s, (const char *)buffer, size, 0);
133 perror("send returned -1");
134 if(errno == ENOTSOCK)
135 printf("ENOTSOCK\n");
143 int Socket::Receive(void *buffer, unsigned long size)
145 int res=recv(s, (char *)buffer, size, 0);
147 perror("recv returned -1"), exit(1);
152 int Socket::SendTo(void *buffer, unsigned long size)
154 int res=sendto(s, (const char *)buffer, size, 0, (const struct sockaddr *)&peer, sizeof(peer));
156 perror("sendto returned -1"), exit(1);
161 int Socket::RecvFrom(void *buffer, unsigned long size)
164 int res=recvfrom(s, (char *)buffer, size, 0, (struct sockaddr *)&peer, &l);
166 perror("recvfrom returned -1"), exit(1);
171 void Socket::SetPeer(struct sockaddr_in data)
173 memcpy(&peer, &data, sizeof(data));
179 struct sockaddr_in Socket::GetPeer(void)
184 struct sockaddr_in Socket::CreatePeer(const char *address, int port)
186 struct sockaddr_in youraddr;
188 printf("Hostname address specified: %s\n", address);
190 youraddr.sin_port=htons(port);
191 youraddr.sin_family=AF_INET;
192 youraddr.sin_addr.s_addr=0;
194 if (isdigit(address[0]))
198 addr=inet_addr(address);
200 memcpy((void *)&(youraddr.sin_addr), (const void *)&addr, sizeof(addr));
204 struct hostent *otherhost;
206 otherhost = gethostbyname(address);
208 if(otherhost == NULL)
211 if (otherhost != NULL)
212 memcpy((void *)&(youraddr.sin_addr), (const void *)otherhost->h_addr_list[0], otherhost->h_length);
219 int Socket::Select(unsigned long seconds, unsigned long microsecs)
224 tv.tv_usec = microsecs;
230 int ret = select(s+1, &rfds, NULL, NULL, &tv);
235 int Socket::Select(unsigned long microsecs)
240 tv.tv_usec = microsecs;
246 int ret = select(s+1, &rfds, NULL, NULL, &tv);
261 Socket * Network::getSocket(int type, int protocol)
263 Socket *tmp=new Socket(type, protocol);