红联Linux门户
Linux帮助

Linux平台下基于TCP/IP协议的C++网络编程初步

发布时间:2015-12-12 09:43:41来源:linux网站作者:郑海波

特点:
1.基于Linux平台的网络编程,最重要的特点是要调用Linux的API函数。这是与windows平台最大的不同。
2.我们将发送的消息封装成结构体,增添了其传递消息的复杂性。
3.我们用的是socket编程,没有用ACE网络编程。


服务器:
[c++ code]
/*
* MyMain.cpp
*
*  Created on: 2012-9-19
*  Author: zhb
*/ 
#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#include <sys/wait.h> 
#include <arpa/inet.h> 
#include <unistd.h> 
#define SERVPORT 3333 
#define BACKLOG 10 
#define MAXSIZE 1024 
/*
*自定义信息
*/ 
typedef struct MyMessage{ 
int ID; 
char info[256]; 
}MyMessage,*pMyMessage; 
int main() { 
int sockfd, client_fd; 
struct sockaddr_in my_addr; 
struct sockaddr_in remote_addr; 
//创建套接字 
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
perror("socket create failed!"); 
exit(1); 

//绑定端口地址 
my_addr.sin_family = AF_INET; 
my_addr.sin_port = htons(SERVPORT); 
my_addr.sin_addr.s_addr = INADDR_ANY; 
bzero(&(my_addr.sin_zero), 8); 
if (bind(sockfd, (struct sockaddr*) &my_addr, sizeof(struct sockaddr))== -1) { 
perror("bind error!"); 
exit(1); 

//监听端口 
if (listen(sockfd, BACKLOG) == -1) { 
perror("listen error"); 
exit(1); 

while (1) { 
//int sin_size = sizeof(struct sockaddr_in); 
socklen_t sin_size = sizeof(struct sockaddr_in); 
if ((client_fd = accept(sockfd,(struct sockaddr*) &remote_addr,&sin_size)) == -1){ 
perror("accept error!"); 
continue; 

printf("Received a connection from %s\n", (char*)inet_ntoa(remote_addr.sin_addr)); 

//子进程段 
if (!fork()){ 
//接受client发送的请示信息 
int rval; 
char buf[MAXSIZE]; 
if ((rval = read(client_fd, buf, MAXSIZE)) < 0) { 
perror("reading stream error!"); 
continue; 

printf("%s\n", buf); 
//向client发送信息 
//char* msg = "Hello,Mr hqlong, you are connected!\n"; 
MyMessage data; 
memset((void *)&data,0,sizeof(MyMessage)); 
data.ID=123; 
strcpy(data.info,"This message come from ServSocket!"); 
if(send(client_fd,(void*)&data,sizeof(MyMessage),0) == -1){ 
perror("send error!"); 

close(client_fd); 
exit(0); 

close(client_fd); 

return 0; 
}


客户端:
[c++ code]
/*
* MyMainClient.cpp
*
*  Created on: 2012-9-19
*  Author: zhb
*/ 
#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#include <sys/wait.h> 
#include <arpa/inet.h> 
#include <unistd.h> 
#define SERVPORT 3333 
#define MAXDATASIZE 100 
#define SERVER_IP "127.0.0.1" 
#define DATA  "this is a client message" 
/*
*自定义信息
*/ 
typedef struct MyMessage{ 
int ID; 
char info[256]; 
}MyMessage,*pMyMessage; 
int main(int argc, char* argv[]) { 
int sockfd, recvbytes; 
//char buf[MAXDATASIZE]; 
MyMessage recvData; 
struct sockaddr_in serv_addr; 

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
perror("socket error!"); 
exit(1); 

bzero(&serv_addr, sizeof(serv_addr)); 
serv_addr.sin_family = AF_INET; 
serv_addr.sin_port = htons(SERVPORT); 
serv_addr.sin_addr.s_addr = inet_addr(SERVER_IP); 

if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr))== -1) { 
perror("connect error!"); 
exit(1); 

write(sockfd, DATA, sizeof(DATA)); 
memset((void *)&recvData,0,sizeof(MyMessage)); 
if ((recvbytes = recv(sockfd, (void *)&recvData,sizeof(MyMessage), 0)) == -1) { 
perror("recv error!"); 
exit(1); 

//buf[recvbytes] = '\0'; 
printf("Received:ID=%d,Info= %s",recvData.ID,recvData.info); 
close(sockfd); 
return 0; 
}


不足:服务器没有进行多线程处理!当多个Client接入时,应考虑多线程编程。


Linux-TCP/IP/UDP/ICMP协议头结构:http://www.linuxdiyf.com/linux/8954.html