1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Ben Parnell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
37 #include "unix-netplay.h"
43 int FCEUD_NetworkConnect(void)
46 struct sockaddr_in sockn;
49 memset(&sockn,0,sizeof(sockn));
50 sockn.sin_family=AF_INET;
51 sockn.sin_port=htons(Port);
53 if((TSocket=socket(AF_INET, SOCK_STREAM, 0))<0)
55 puts("Error creating socket.");
59 if(netplay==1) /* Be a server. */
61 sockn.sin_addr.s_addr=INADDR_ANY;
62 if(bind(TSocket, (struct sockaddr *)&sockn, sizeof(sockn))<0)
65 puts("Error binding to socket.");
68 if(listen(TSocket, 1)<0)
70 puts("Error listening on socket.");
75 socklen_t len=sizeof(sockn);
77 printf("Accepting connection on port %d...\n",Port);
78 if((Socket=accept(TSocket,(struct sockaddr *)&sockn,&len))<0 )
80 puts("Error accepting a connection.");
88 else /* Connect as a client if not a server. */
92 if((sockn.sin_addr.s_addr=inet_addr(netplayhost))==INADDR_NONE)
94 if(!(Host=gethostbyname(netplayhost)))
96 puts("Error getting network host entry.");
99 memcpy(&sockn.sin_addr,Host->h_addr,Host->h_length);
101 printf("Attempting to connect to %s...\n",netplayhost);
102 if( connect(TSocket, (struct sockaddr *)&sockn, sizeof(sockn)) <0 )
104 puts("Error connecting to remote host.");
114 /* 0 on failure, 1 on success, -1 if it would block and blocking is not
118 int FCEUD_NetworkRecvData(uint8 *data, uint32 len, int block)
125 t=recv(Socket,temp,32,MSG_PEEK|MSG_DONTWAIT);
128 if(errno!=EAGAIN) return(0);
134 return(recv(Socket,data,len,0)==len);
138 int t=recv(Socket,data,len,MSG_DONTWAIT);
141 if(errno==EAGAIN) // Would block
152 /* 0 on failure, 1 on success. This function should always block. */
154 int FCEUD_NetworkSendData(uint8 *Value, uint32 len)
157 return(send(Socket,Value,len,0)==len);
163 void FCEUD_NetworkClose(void)