1 / 9

Network Programming using NetLink Sockets C++ Library

Network Programming using NetLink Sockets C++ Library. http://sourceforge.net/projects/netlinksockets/. For Two Hosts to Communicate …. Host1. Host2. This is similar to File I/O: fopen(), fread()/fwrite(), fclose().

bobt
Download Presentation

Network Programming using NetLink Sockets C++ Library

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Network ProgrammingusingNetLink Sockets C++ Library http://sourceforge.net/projects/netlinksockets/

  2. For Two Hosts to Communicate … Host1 Host2 • 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. 1. Establish a connection (socket) 2. Send/Receive 3. Tear down the connection

  3. 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; }

  4. 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; }

  5. 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

  6. 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”

  7. Two Programs Talks • Remember to start “server.exe” first.

  8. 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.

  9. Modify client.cpp No change is needed on server.cpp, but you must start server.exe before another client.exe trying to establish the connection. #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; }

More Related