Dit heb ik dus als code gevonden op een internet site, ik snap ook wel hoe alles in elkaar zit... maar toch fouten erin.... (zie onderaan voor de fouten:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
| #include <iostream.h>
#include <winsock2.h>
const unsigned int maxchars = 200; //maximum number of chars for the buffer (i/o)
//The client
void Client()
{
char dest_ip[15]; //destination IP
cout << "Destination IP: ";
cin >> dest_ip;
cout << endl;
SOCKET go; //create the sending socket structure
if(go = socket(AF_INET,SOCK_STREAM,0))
cout << "--- Sending socket created successfully!\n";
else
{
cout << "Can't create sending socket! Aborting!\n";
WSACleanup();
exit(0);
}
sockaddr_in target;
target.sin_family = AF_INET;
target.sin_port = htons(7979); //sending port
target.sin_addr.s_addr = inet_addr(dest_ip);
if(connect(go,(LPSOCKADDR)&target,sizeof(target)) == SOCKET_ERROR)
{
cout << "Can't connect! Aborting!\n";
WSACleanup();
exit(0);
}
else
{
cout << "--- Connected successfully!\n\n";
cout << "-| Enter 000 when you want to end the session!\n\n";
char buff[maxchars];
while(buff[0] != '0' || buff[1] != '0' || buff[2] != '0')
{
cin.getline(buff,maxchars);
cout << "Enter data: ";
if(buff[0] != '0' || buff[1] != '0' || buff[2] != '0')
send(go,buff,sizeof(buff),0);
else
{
send(go,buff,sizeof(buff),0);
closesocket(go);
cout << "-> Session closed!\n";
WSACleanup();
exit(0);
}
}
}
}
void Server()
{
SOCKET come;
if(come = socket(AF_INET,SOCK_STREAM,0))
cout << "--- Receiving socket created successfully!\n";
else
{
cout << "Can't create receiving socket! Aborting!\n";
WSACleanup();
exit(0);
}
sockaddr_in addr; //the address structure for a TCP socket
addr.sin_family = AF_INET; //Address family internet
addr.sin_port = htons(7979); //Receiving port
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(come,(LPSOCKADDR)&addr,sizeof(addr)) == SOCKET_ERROR)
{ //error
cout << "Error binding! Aborting!\n";
WSACleanup(); //unload WinSock
exit(0); //quit
}
if(listen(come,1) == SOCKET_ERROR)
{ //error. unable to listen
cout << "Error listening! Aborting!\n";
closesocket(come);
WSACleanup();
exit(0);
}
else
{
SOCKET client; //socket handles to clients
client = accept(come,NULL,NULL);
if(client == INVALID_SOCKET)
{ //error accepting the connection
cout << "Can't accept connection! Aborting!\n";
closesocket(come);
WSACleanup();
exit(0);
}
else
{
cout << endl;
char buffer[maxchars]; //buffer for incomming messages
while(buffer[0] != '0' || buffer[1] != '0' || buffer[2] != '0')
{
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
recv(client,buffer,sizeof(buffer),0);
if(buffer[0] != '0' || buffer[1] != '0' || buffer[2] != '0')
{
if(strlen(buffer) > 0)
cout << "Received: " << buffer << endl;
}
else
{
closesocket(come);
cout << "-> Session closed!\n" << endl;
WSACleanup();
exit(0);
}
}
}
}
}
void main()
{
WSADATA w;
if(WSAStartup(0x0202,&w))
{
cout << "Can't initialize WinSock. Aborting!\n";
exit(0);
}
else
cout << "--- WinSock initialized successfully!\n";
if(w.wVersion != 0x0202)
{ //wrong version
cout << "Wrong version.Aborting!\n";
WSACleanup(); //Unload ws2_32.dll
exit(0);
}
else
cout << "--- Version 2.2 accepted!\n";
char o;
cout << endl;
while(o != '1' && o!= '2' && o != '3')
{
cout << "1 - Run Server\n";
cout << "2 - Run Client\n";
cout << "3 - Exit\n";
cout << "Enter 1,2 or 3: ";
cin >> o;
}
switch(o)
{
case '1':
Server();
break;
case '2':
Client();
break;
case '3':
exit(0);
break;
};
} |
Fouten:
TCPIP error LNK2001: unresolved external symbol "int __stdcall bind(unsigned int,struct sockaddr const *,int)" (?bind@@$$J212YGHIPBUsockaddr@@H@Z)
TCPIP error LNK2001: unresolved external symbol "int __stdcall closesocket(unsigned int)" (?closesocket@@$$J14YGHI@Z)
TCPIP error LNK2001: unresolved external symbol "int __stdcall connect(unsigned int,struct sockaddr const *,int)" (?connect@@$$J212YGHIPBUsockaddr@@H@Z)
etc etc en dan zo'n 10 meer ervan......