麻烦列出在linux或unix平台下C++编套接字通讯程序要用到哪些库或头文件

tangrh 2005-01-17 09:00:05
我需要在linux下用C++写一个TCP程序,采用标准的bsd套接口API,技术上估计不会有问题,关键是由于以前没有搞过C和C++,所以对头文件和标准库都不熟,哪位帮忙列举一下,谢谢
...全文
126 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangrh 2005-01-17
  • 打赏
  • 举报
回复
好啊,有点眉目了
pacman2000 2005-01-17
  • 打赏
  • 举报
回复
和C的头文件是一样的啊!用到什么函数,man一下看看就知道要哪些头文件了。
whoho 2005-01-17
  • 打赏
  • 举报
回复
客户端
#include <cstdio>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <memory.h>
#include <arpa/inet.h>

int main()
{
int sock;
sockaddr_in addr;
socklen_t addr_len = sizeof(addr);

//create DATAGRAM socket
sock = socket(AF_INET, SOCK_DGRAM, 0);

//binding
memset(&addr, 0, addr_len);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(1234);
bind(sock, (sockaddr*)&addr, addr_len);

//send
std::string content("######### Hello Server ######### ");
content += '\0';
sendto(sock, content.c_str(), content.size(), 0,
(sockaddr*)&addr, addr_len);
//get reply
sockaddr_in srv_addr;
char buf[1024];
int n = recvfrom(sock, buf, 1024, 0,
(sockaddr*)&srv_addr, &addr_len);
printf("%d bytes of reply recieved, content: \n%s\n", n, buf);

}

whoho 2005-01-17
  • 打赏
  • 举报
回复
给你一个简单的udp例子,两个文件,先是服务器
#include <cstdio>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <memory.h>
#include <cassert>

int main()
{
int sock;
sockaddr_in addr;
socklen_t addr_len = sizeof(addr);

//create DATAGRAM socket
sock = socket(AF_INET, SOCK_DGRAM, 0);

//binding
memset(&addr, 0, addr_len);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(1234);
bind(sock, (sockaddr*)&addr, addr_len);

while(true)
{
sockaddr_in client_addr;
char buf[1024];

//recieve
int n = recvfrom(sock, buf, 1024, 0,
(sockaddr*)&client_addr, &addr_len);
printf("%d bytes recieved, content: \n%s\n", n, buf);

//reply
std::string reply("you sent: ");
reply += buf; reply += "\nacknowleged\n";
reply+='\0';
printf("reply content is:\n%s ", reply.c_str());
sendto(sock, reply.c_str(), reply.size(), 0,
(sockaddr*)&client_addr, addr_len);

}
}


whoho 2005-01-17
  • 打赏
  • 举报
回复

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

这三个基本够了
tengulre 2005-01-17
  • 打赏
  • 举报
回复
#include <sys/types.h>
#include <sys/socket.h>
tangrh 2005-01-17
  • 打赏
  • 举报
回复
我自己找了篇文章,先把它列出来吧
sys/types.h 必要的类型定义头文件
errno.h 全局错误号头文件
sys/socket.h sockaddr结构,系统函数原型和常数
netdb.h 网络信息查找函数原型和结构
netinet/in.h sockaddr_in结构,字节序宏定义
arpa/inet.h 工具函数原型
tangrh 2005-01-17
  • 打赏
  • 举报
回复
楼上:
你有没有用过epoll?另外你是如何轮询套接字的
Neo_Liu 2005-01-17
  • 打赏
  • 举报
回复
这是我写的一个代理程序引用的头文件

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <unistd.h>
#include <netdb.h>
#include <pthread.h>
#include <sys/select.h>
#include <assert.h>

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧