Any quick guide to understand networking and use it in programming? [closed]
is there any quick guide to understand basic concept of computer networking like layers of networking tcp/ip and how to use it in programming language like c ? i am not talking about books but some tutorials availab开发者_Go百科le on net.
I just read through this yesterday. It gives great explanations about the network stack and how to program with it via C++ C.
http://beej.us/guide/bgnet/output/html/multipage/index.html
It's more or less an eBook, but there are a lot of tutorials and examples.
Hope that helps!
wikipedia is good for understanding layers. http://en.wikipedia.org/wiki/Internet_Protocol_Suite
And for how to use it in C, see beej's guide. http://beej.us/guide/bgnet/
If you really want a good insight into TCP/IP, then unfortunately I need to point you at this book:
"TCP/IP Illustrated, Vol. 1: The Protocols" by W. Richard Stevens
Well, I couldn't find any great ones but I do have a TCP/IP sockets in C book which I spent a long time going through - and I made an all-in one sample app which splits processes and does a TCP/IP connection to local-host. You can split the app into 2 separate ones and run them on different PCs and it works great. Its well commented, so here it is - hope it helps :)
* EDIT* this site has the contents of the textbook I used to learn this, it'll tell you everything and has great code samples:
http://cs.baylor.edu/~donahoo/practical/CSockets/
//-----------------------------------------------------------------------------------------------------
//
// Author : John Humphreys
//
// File Name : UnixTcp.cpp
//
// Description : This file serves as an all-in-one client/server TCP example. It uses the Unix fork()
// command to create an additional process. The original process is used as the client
// in the communication and the new process is used as the server. The basic steps of
// setting up IPV4 TCP sockets are explained in commenting, and output is provided
// within the code so the user may compile and run it and observe the steps executing
// in practice.
//
// Purpose : This file is intended to serve as a very basic example of how to use TCP sockets in
// a UNIX environment. In addition to this, it is intended to teach the reader/user
// basic C-style error reporting and handling as programmers coming from C++ and other
// object oriented backgrounds sometimes have trouble with this.
//-----------------------------------------------------------------------------------------------------
#include <iostream>
#include <unistd.h>
#inlcude <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
const char* message = "Hello Server!"; //The message we're going to send.
const char* servIP = "127.0.0.1"; //Loopback interface.
//const char* servIP = "192.168.0.1"; //Use for perror no-route-to-host example.
//const char* servIP = "172.30.34.246"; //Local interface IP - works like loopback.
int servPort = 50001; //Server TCP port in dynamic numbers range.
const int BUFSIZE = 256; //Maximum size of receive buffer for server.
const int MAXCLIENTS = 10;
int main()
{
//Fork() to create two processes - each has own memory space and variable copies, so we can make
//them communicate over sockets much like two computers would.
if (fork())
{
std::cerr << "[CLIENT] Process Running." << std::endl;
//Sleep for a second so the server process will be ready for the connection.
sleep(1);
//Create an IPV4 TCP stream socket.
int client_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//Set up the address structure for the server w/ valid address pointer and port number.
sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
inet_pton(AF_INET, servIP, &servAddr.sin_addr.s_addr);
servAddr.sin_port = htons(servPort);
//Connect to the server using the socket and the server address.
int result = connect(client_sock, reinterpret_cast<sockaddr*>(&servAddr), sizeof(servAddr));
if (result < 0)
{
perror("[CLIENT] Connect failed: ");
}
//Transmit the message - leave a message on the console for this example.
std::cerr << "[CLIENT] Transmitting message" << message << "." << std::endl;
send(client_sock, message, strlen(message), 0);
}
else
{
std::cerr << "[SERVER] Process running." << std::endl;
std::cerr << "[SERVER] Creating socket..." << std::endl;
//Create an IPV4 TCP stream socket.
int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//Set up the address structure for the server w/ valid address pointer and port number.
sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_address.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(servPort);
std::cerr << "[SERVER] Binding to socket..." << std::endl;
std::cerr << "[SERVER] Listening on socket..." << std::endl;
//Bind the server socket to the address and listen on the port for up to MAXCLIENTS connections.
bind(serv_sock, reinterpret_cast<sockaddr*>(&servAddr), sizeof(servAddr));
listen(serv_sock, MAXCLIENTS);
//Create a client address structure for use in accept().
sockaddr_in clientAddr;
socklen_t clientAddrLen = sizeof(clientAddr);
std::cerr << "[SERVER] Waiting to accept a connection..." << std::endl;
//Call accept to wait for a client to make a connection - use clientAddr to get client details.
int client_sock = accept(servSock, reinterpret_cast<sockaddr*>(&clientAddr), &clientAddrLen);
std::cerr << "[SERVER] Connection accepted. Waiting for messages from clinet..." << std::endl;
//Declare a buffer of BUFSIZE (max receive size).
char buffer[BUFSIZE];
//Call recv to wait for data on the socket. Maximum of BUFSIZE will be written into buffer.
size_t bytes_received = recv(client_sock, buffer, BUFSIZE, 0);
//Output the size and contents of the received message to the user.
std::cerr << "[SERVER] Received " << bytes_received << " bytes from client." << std::endl;
std::cerr << "[SERVER] Message was: " << buffer << "." << std::endl;
}
}
To understand the concepts of networking, you'll first have to go through this
http://www.aw-bc.com/kurose_ross/
I mean you need a reference to grasp the concepts, then you can move on to the online sites available for the network programming in c. The book is necessary because it builds the base.
The Beej's guide is the best for unix network programming in C. But I prefer this book
http://books.google.com/books?id=ptSC4LpwGA0C&printsec=frontcover&dq=unix+network+programming&hl=en&ei=2ZYpToL7No6WtweE5KHXAg&sa=X&oi=book_result&ct=result&resnum=1&sqi=2&ved=0CCkQ6AEwAA#v=onepage&q&f=false
Because this is known as the bible of network programming.
http://www.linuxhowtos.org/C_C++/socket.htm
精彩评论