9
Network Programming using NetLink Sockets C++ Library http://sourceforge.net/projects/ netlinksockets/

Network Programming using NetLink Sockets C++ Library

Embed Size (px)

Citation preview

Page 1: Network Programming using NetLink Sockets C++ Library

Network Programmingusing

NetLink Sockets C++ Libraryhttp://sourceforge.net/projects/netlinksockets/

Page 2: Network Programming using NetLink Sockets C++ Library

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

Page 3: Network Programming using NetLink Sockets C++ Library

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;

}

Page 4: Network Programming using NetLink Sockets C++ Library

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;

}

Page 5: Network Programming using NetLink Sockets C++ Library

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

Page 6: Network Programming using NetLink Sockets C++ Library

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”

Page 7: Network Programming using NetLink Sockets C++ Library

Two Programs Talks• Remember to start

“server.exe” first.

Page 8: Network Programming using NetLink Sockets C++ Library

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.

Page 9: Network Programming using NetLink Sockets C++ Library

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.