Upload
sophia-gilbert
View
220
Download
0
Embed Size (px)
Citation preview
Network Programmingusing
NetLink Sockets C++ Libraryhttp://sourceforge.net/projects/netlinksockets/
For Two Hosts to Communicate …
• This is similar to File I/O: fopen(), fread()/fwrite(), fclose().• Let us assume Host2 is a server, which is waiting for request to establish a
connection.
Host1 Host2
1. Establish a connection (socket)
2. Send/Receive
3. Tear down the connection
server.cpp
#include <iostream>
using std::cout;
#include <netlink/socket.h>
int main() {
NL::init();
cout << "\nStarting server...";
cout.flush();
NL::Socket server(5000);
NL::Socket* clientConnection = server.accept();
char buffer[256];
buffer[255] = '\0';
while(clientConnection->read(buffer,255)) {
cout << "\nMessage: " << buffer;
cout.flush();
}
delete clientConnection;
cout << "\nClient disconnected. Exit...";
return 0;
}
client.cpp#include <iostream>
#include <string.h>
using std::cout;
using std::cin;
#include <netlink/socket.h>
int main() {
NL::init();
cout << "\nEcho Client...";
cout.flush();
NL::Socket socket("localhost", 5000);
char input[256];
input[255] = '\0';
while(strcmp(input, "exit")) {
cout << "\n--> ";
cin.getline(input, 255);
socket.send(input, strlen(input) + 1);
}
return 0;
}
Compile client.cpp• Download http://ipv6.twbbs.org/Course/CS102/netLink_v1.0.0.zip• Uncompress the to a directory (e.g. N:\NetLink).• Create a new Visual C++ project
– Add Existing Items (Alt + Shift + A)• client.cpp (in N:\NetLink\examples\socket)• core.cpp & socket.cpp (in N:\Netlink\src)
– Alt-F7 to modify the Configuration Properties• C/C++ - Additional Include Directories:
N:\netLink\include• Linker - Input - Additional Dependencies:
ws2_32.lib• Press <F7> to compile.
• You now obtain a “client.exe” under C:\Users\yourname\Documents\Visual Studio 2010\Projects\client\Debug
Compile and Run server.cpp
• Do the same to compile server.cpp.• Open a Command Window and change to the
directory “C:\Users\yourname\Documents\Visual Studio 2010\Projects\server\Debug”
• Run “server.exe”.• Open another Command Window and change
directory to “C:\Users\yourname\Documents\Visual Studio 2010\Projects\client\Debug”
• Run “client.exe”
Two Programs Talks• Remember to start
“server.exe” first.
Talk to Other Hosts on the Internet
• You may reach other hosts on the Internet, if you want their IP addresses.
• An IP address is a unique id for each host on the Internet.
• You may run “ipconfig” to show the IP address of your PC.
Modify client.cpp#include <iostream>
#include <string.h>
using std::cout;
using std::cin;
#include <netlink/socket.h>
int main() {
NL::init();
cout << "\nEcho Client...";
cout.flush();
NL::Socket socket("10.20.10.159", 5000);
char input[256];
input[255] = '\0';
while(strcmp(input, "exit")) {
cout << "\n--> ";
cin.getline(input, 255);
socket.send(input, strlen(input) + 1);
}
return 0;
}
No change is needed on server.cpp, but you must start server.exe before another client.exe trying to establish the connection.