read(buffer,255)) { cout << "\nMessage: " << buffer; cout.flush(); } delete clientConnection; cout << "\nClient disconnected. Exit..."; return 0; }"> read(buffer,255)) { cout << "\nMessage: " << buffer; cout.flush(); } delete clientConnection; cout << "\nClient disconnected. Exit..."; return 0; }">
Download presentation
Presentation is loading. Please wait.
Published bySophia Gilbert Modified over 9 years ago
1
Network Programming using NetLink Sockets C++ Library http://sourceforge.net/projects/netlinksockets/
2
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. Host1Host2 1. Establish a connection (socket) 2. Send/Receive 3. Tear down the connection
3
server.cpp #include using std::cout; #include 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 using std::cout; using std::cin; #include 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 "; 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.ziphttp://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 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 #include using std::cout; using std::cin; #include 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 "; 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.