root/deus/comm1.cpp

Revision 8, 7.2 kB (checked in by pantley2, 4 years ago)

added old DEUS project to SVN

Line 
1
2 #include "structures.h"
3
4 //#include <Winsock2.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8
9 // the server ip we are trying to connect to
10 #define _SERVER_IP "10.0.10.159"
11 #define PORT 6000
12
13 // the global world state object is real_world
14
15
16 // int recievedata()
17 //  builds objects --- need to find out how to build
18 // returns
19 //        0 if failure
20 //        -1 if success
21
22 int recievedata()
23 {
24     sockaddr_in name;
25     WSADATA WSAData;
26         SOCKET sock;
27    
28     // init socket
29     if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0)
30         printf("Error initializing WinSocket\n");
31     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
32         printf("Invalid Socket!\n");
33    
34     name.sin_family = AF_INET; // TCP/IP
35     name.sin_port = htons(PORT); // Port number
36     name.sin_addr.S_un.S_addr = inet_addr(_SERVER_IP); // Server IP
37     
38     // establish connection to the server
39     if (connect(sock, (LPSOCKADDR) &name, sizeof(sockaddr_in)) == SOCKET_ERROR)
40     {
41         printf("Connect error!\n");
42         closesocket(sock);
43         return 0;
44     }
45    
46    
47     // -- BEGIN RECEIVE --
48     // resp for storing the recieved string
49     char buff[512]; //, resp[512];
50         // try and recieve the first string[16] "WORLD"
51     int recvd = recv(sock, buff, 16, 0);
52     if (strcmp(buff,"WORLD") !=0)
53         {
54                 // case of failure as first word recieved is not "WORLD"
55                 closesocket(sock);
56                 return 0;
57         }
58         // if successful then recieve the number of NPCs
59         int numNPC=0;
60         recvd = recv(sock, &buff[0], 4, 0);
61         char temp[4];
62         strncpy(temp,&buff[0],4);
63         numNPC = int(temp);
64     if (numNPC<0)
65         {
66                 // case of failure as first word recieved is not "WORLD"
67                 closesocket(sock);
68                 return 0;
69         }
70        
71         // if successful get the NPC name and attrib
72         for (int i=0; i<numNPC ; i++)
73         {
74                 // try and recieve the NPCname string[32]
75                 
76                 int recvd = recv(sock, buff, 32, 0);
77                 if (strcmp(buff,"") ==0) // cannot have a noname NPC
78                 {
79                         // case of failure as first word recieved is not "WORLD"
80                         closesocket(sock);
81                         return 0;
82                 }
83                 char NPCname[32];
84                 strncpy(&NPCname[0], &buff[0], 32);
85
86                 // if successful then recieve the location x
87                 int loc_x=0;
88                 recvd = recv(sock, &buff[0], 4, 0);
89                 char temp[4];
90                 strncpy(temp,&buff[0],4);
91                 loc_x=int(temp);
92                 if (loc_x<0 || loc_x>299) // location between 0 and 299
93                 {
94                         // case of failure as first word recieved is not "WORLD"
95                         closesocket(sock);
96                         return 0;
97                 }
98                 // if successful then recieve the location y
99                 int loc_y=0;
100                 recvd = recv(sock, &buff[0], 4, 0);
101                 strncpy(temp,&buff[0],4);
102                 loc_y=int(temp);
103                 if (loc_y<0 || loc_y>299) // location between 0 and 299
104                 {
105                         // case of failure as first word recieved is not "WORLD"
106                         closesocket(sock);
107                         return 0;
108                 }
109                 // ********************* addNPC --------------------
110                 NPC newNPC;
111                 newNPC.xcoord=loc_x;
112                 newNPC.ycoord=loc_y;
113                 newNPC.name = NPCname;
114                
115                 // --------------------- NPC attributes --------------------
116                 // now recieve the number of attributes
117                 int numNPCattrib=0;
118                 recvd = recv(sock, &buff[0], 4, 0);
119                 strncpy(temp,&buff[0],4);
120                 numNPCattrib=int(temp);
121                 if (numNPCattrib<0) // location between 0 and 299
122                 {
123                         // case of failure as first word recieved is not "WORLD"
124                         closesocket(sock);
125                         return 0;
126                 }
127                 for(int k=0; k<numNPCattrib ; k++)
128                 {
129                         // get name of attrib
130                         int recvd = recv(sock, buff, 16, 0);
131                         if (strcmp(buff,"") ==0) // cannot have a noname NPC attribute
132                         {
133                                 // case of failure as first word recieved is not "WORLD"
134                                 closesocket(sock);
135                                 return 0;
136                         }
137                         char NPCattribNAME[16];
138                         strncpy(&NPCattribNAME[0], &buff[0], 16);
139                         int attribute_value=0;
140                         recvd = recv(sock, &buff[0], 4, 0);
141                         strncpy(temp,&buff[0],4);
142                     attribute_value=int(temp);
143                         if (attribute_value<0) // cannot have negative attribute values
144                         {
145                                 // case of failure as first word recieved is not "WORLD"
146                                 closesocket(sock);
147                                 return 0;
148                         }
149                         string name = NPCattribNAME;
150                         // ******************* add NPC attribute
151                         newNPC.attributes.insert(std::pair<string, int>(name,attribute_value));
152                        
153
154                 }
155                 // --------------------- done attrib --------------------------
156                 // --------------------- relationships -------------------------
157                 int numNPCrelationship=0;
158                 recvd = recv(sock, &buff[0], 4, 0);
159                 strncpy(temp,&buff[0],4);
160                 numNPCrelationship=int(temp);
161                 if (numNPCrelationship<0) // location between 0 and 299
162                 {
163                         // case of failure as first word recieved is not "WORLD"
164                         closesocket(sock);
165                         return 0;
166                 }
167                 for(k=0; k<numNPCrelationship ; k++)
168                 {
169                         // get name of attrib
170                         recvd = recv(sock, buff, 16, 0);
171                         if (strcmp(buff,"") ==0) // cannot have a noname NPC attribute
172                         {
173                                 // case of failure as first word recieved is not "WORLD"
174                                 closesocket(sock);
175                                 return 0;
176                         }
177                         char NPCrelationshipNAME[16];
178                         strncpy(&NPCrelationshipNAME[0], &buff[0], 16);
179                         int group_value=0;
180                         recvd = recv(sock, &buff[0], 4, 0);
181                         strncpy(temp,&buff[0],4);
182                         group_value=int(temp);
183                         if (group_value<0) // cannot have negative attribute values
184                         {
185                                 // case of failure as first word recieved is not "WORLD"
186                                 closesocket(sock);
187                                 return 0;
188                         }
189                        
190                         int relationship=0;
191                         recvd = recv(sock, &buff[0], 4, 0);
192                         strncpy(temp,&buff[0],4);
193                         relationship=int(temp);
194                         if (relationship<0) // cannot have negative attribute values
195                         {
196                                 // case of failure as first word recieved is not "WORLD"
197                                 closesocket(sock);
198                                 return 0;
199                         }
200
201                         // ******************* add NPC relationships
202                         std::pair <string, int> namegroupvalue(NPCrelationshipNAME, group_value);
203                         newNPC.relationships.insert(std::pair<std::pair<string,int> , int>(namegroupvalue,relationship));
204                 }
205                 // ------------------- done relationships ----------------------
206                 // add the built NPC object
207                 real_world->NPCs.push_back(newNPC);
208
209         }
210        
211
212
213    
214     // Close open socket
215     closesocket(sock);
216         return -1;
217 }
218
219
220
221 // int senddata(string query)
222 //      qeury is the string to be sent
223 // returns
224 //        0 if failure
225 //        -1 if success
226 //  if need to send int please convert to string using
227 //  char* itoa( int value,  char* buffer, int radix ); present in stdlib.h
228 int senddata(string query)
229 {
230         SOCKET sock;
231     sockaddr_in name;
232     WSADATA WSAData;
233    
234     // init socket
235     if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0)
236         printf("Error initializing WinSocket\n");
237     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
238         printf("Invalid Socket!\n");
239    
240     name.sin_family = AF_INET; // TCP/IP
241     name.sin_port = htons(PORT); // Port number
242     name.sin_addr.S_un.S_addr = inet_addr(_SERVER_IP); // Server IP
243     
244     // establish connection to the server
245     if (connect(sock, (LPSOCKADDR) &name, sizeof(sockaddr_in)) == SOCKET_ERROR)
246     {
247         printf("Connect error!\n");
248         closesocket(sock);
249         return 0;
250     }
251    
252    
253     // BEGIN SEND
254     // send query
255     // string query, response;
256     //sprintf(buff, "%d", s.length());
257     // query = string(buff) + "X" + s;
258     if (send(sock, query.c_str(), query.length(), 0) == 0)
259     {
260         printf("Error sending\n");
261         closesocket(sock);
262         return 0;
263     }
264     closesocket(sock);   
265     return -1;
266 }
267
268 int temp;
Note: See TracBrowser for help on using the browser.