基于Server /Client 模式:
感觉写的比较简单清晰,应该看注释就大概可以理解
server.cpp
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#define MAXHOSTNAME 256
#define BUFFSIZE 512
using namespace std;
int main()
{
struct sockaddr_in socketInfo;
char sysHost[MAXHOSTNAME+1]; // Hostname of this computer we are running on
struct hostent *hPtr;
int socketHandle;
int portNumber = 8080;
bzero(&socketInfo, sizeof(sockaddr_in)); // Clear structure memory
// Get system information
gethostname(sysHost, MAXHOSTNAME); // Get the name of this computer we are running on
cout<<sysHost<<endl;
if((hPtr = gethostbyname(sysHost)) == NULL)
{
cerr << "System hostname misconfigured." << endl;
exit(EXIT_FAILURE);
}
// create socket
if((socketHandle = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
close(socketHandle);
exit(EXIT_FAILURE);
}
// Load system information into socket data structures
socketInfo.sin_family = AF_INET;
socketInfo.sin_addr.s_addr = htonl(INADDR_ANY); // Use any address available to the system
socketInfo.sin_port = htons(portNumber); // Set port number
// Bind the socket to a local socket address
if( bind(socketHandle, (struct sockaddr *) &socketInfo, sizeof(socketInfo)) < 0)
{
close(socketHandle);
perror("bind");
exit(EXIT_FAILURE);
}
if(listen(socketHandle, 1)<0){//maximum of pending connections
cout<<"listening failed"<<endl;
close(socketHandle);
}
//cout<<"my skid "<<socketHandle<<endl;
cout<<"start listening"<<endl;
int socketConnection;
while(1){
if( (socketConnection = accept(socketHandle, NULL, NULL)) < 0)
{
exit(EXIT_FAILURE);
}
//close(socketHandle);
int rc = 0; // Actual number of bytes read
char buf[512];
// rc is the number of characters returned.
// Note this is not typical. Typically one would only specify the number
// of bytes to read a fixed header which would include the number of bytes
// to read. See "Tips and Best Practices" below.
//cout<<"client skid "<<socketConnection<<endl;
rc = recv(socketConnection, buf,BUFFSIZE, 0); //发送端套接字描述符
buf[rc]= (char) NULL; // Null terminate string
cout << "Number of bytes read: " << rc << endl;
cout << "Received: " << buf << endl;
strcpy(buf,"hahahah");
cout<<"write "<<buf<<"num:"<<BUFFSIZE<<endl;
send(socketConnection, buf, BUFFSIZE, 0);
}
close(socketHandle);
}
client.cpp
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#define MAXHOSTNAME 256
#define BUFFSIZE 512
using namespace std;
int main()
{
struct sockaddr_in remoteSocketInfo;
struct hostent *hPtr;
int socketHandle;
const char *remoteHost="localhost";
int portNumber = 8080;
bzero(&remoteSocketInfo, sizeof(sockaddr_in)); // Clear structure memory
// Get system information
if((hPtr = gethostbyname(remoteHost)) == NULL)
{
cerr << "System DNS name resolution not configured properly." << endl;
cerr << "Error number: " << ECONNREFUSED << endl;
exit(EXIT_FAILURE);
}
// create socket
if((socketHandle = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
close(socketHandle);
exit(EXIT_FAILURE);
}
cout<<"client skid "<<socketHandle<<endl;
// Load system information into socket data structures
memcpy((char *)&remoteSocketInfo.sin_addr, hPtr->h_addr, hPtr->h_length);
remoteSocketInfo.sin_family = AF_INET;
remoteSocketInfo.sin_port = htons((u_short)portNumber); // Set port number
if(connect(socketHandle, (struct sockaddr *)&remoteSocketInfo, sizeof(sockaddr_in)) < 0)//0 means success
{
close(socketHandle);
exit(EXIT_FAILURE);
}
int rc = 0; // Actual number of bytes read by function read()
char buf[BUFFSIZE];
strcpy(buf,"Message to send");
cout<<"send length:"<<strlen(buf)+1<<endl;
send(socketHandle, buf, BUFFSIZE, 0);
// rc is the number of characters returned.
// Note this is not typical. Typically one would only specify the number
// of bytes to read a fixed header which would include the number of bytes
// to read. See "Tips and Best Practices" below.
cout<<"read length"<<BUFFSIZE<<endl;
rc = recv(socketHandle, buf, BUFFSIZE, 0);
//buf[rc]= (char) NULL; // Null terminate string
cout << "Number of bytes read: " << rc << endl;
cout << "Received: " << buf << endl;
}