求一C语言写的tcp程序的源码,程序越简单越好,C++也可以,最好带注释
3个回答
展开全部
最简单的那种?我给你写一个=,=
//服务器
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
main()
{
//创建套接字
int serverSocket= socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in server_addr;
struct sockaddr_in clientAddr;
int addr_len = sizeof(clientAddr);
int client;
char buffer[200];
//创建地址
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family =AF_INET;
server_addr.sin_port = htons(5555);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
//绑定
bind(serverSocket,(struct sockaddr *)&server_addr,sizeof(server_addr));
//帧听
listen(serverSocket,5);
printf("客户端发过来的 :\n");
//接收
client=accept(serverSocket,(sockaddr *)&clientAddr,(socklen_t*)&addr_len);
printf("客户端发过来的 :\n");
while(1)
{
if(recv(client,buffer,sizeof(buffer),0)>0)
printf("客户端发过来的 : %s\n",buffer);
}
return 0;
}
//客户端
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
main()
{
struct sockaddr_in serverAddr;
int clientSocket=socket(AF_INET,SOCK_STREAM,0);
char sendbuf[200];
//创建地址信息
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(5555);
serverAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
//连接服务器
connect(clientSocket,(sockaddr*)&serverAddr,sizeof(serverAddr));
printf("连接目标主机中.....\n连接完成......\n");
//
while(1)
{
printf("请输入发给服务器的数据 : ");
scanf("%s",sendbuf);
//strcmp函数作用,比较两个东东(按ASCII值大小相比较),相同就==0。
if(strcmp(sendbuf,"x")==0)
break;
send(clientSocket,sendbuf,sizeof(sendbuf),0);
}
close(clientSocket);
return 0;
}
//服务器
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
main()
{
//创建套接字
int serverSocket= socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in server_addr;
struct sockaddr_in clientAddr;
int addr_len = sizeof(clientAddr);
int client;
char buffer[200];
//创建地址
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family =AF_INET;
server_addr.sin_port = htons(5555);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
//绑定
bind(serverSocket,(struct sockaddr *)&server_addr,sizeof(server_addr));
//帧听
listen(serverSocket,5);
printf("客户端发过来的 :\n");
//接收
client=accept(serverSocket,(sockaddr *)&clientAddr,(socklen_t*)&addr_len);
printf("客户端发过来的 :\n");
while(1)
{
if(recv(client,buffer,sizeof(buffer),0)>0)
printf("客户端发过来的 : %s\n",buffer);
}
return 0;
}
//客户端
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
main()
{
struct sockaddr_in serverAddr;
int clientSocket=socket(AF_INET,SOCK_STREAM,0);
char sendbuf[200];
//创建地址信息
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(5555);
serverAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
//连接服务器
connect(clientSocket,(sockaddr*)&serverAddr,sizeof(serverAddr));
printf("连接目标主机中.....\n连接完成......\n");
//
while(1)
{
printf("请输入发给服务器的数据 : ");
scanf("%s",sendbuf);
//strcmp函数作用,比较两个东东(按ASCII值大小相比较),相同就==0。
if(strcmp(sendbuf,"x")==0)
break;
send(clientSocket,sendbuf,sizeof(sendbuf),0);
}
close(clientSocket);
return 0;
}
更多追问追答
追问
g:\tcp\新建文件夹\cpp1.cpp(9) : fatal error C1083: Cannot open include file: 'netdb.h': No such file or directory错误,是不是要用wintc才行,我想顺便问下,怎样去运行服务端和客户端,还是只要在上面编译成功运行就行,就是怎样用这两程序
追答
先运行服务器,再运行客户端啊。。。
然后客户端就可以发信息给服务器了
这个都是在LINUX下编程的吧=。=
展开全部
装个Linux自己有不懂的函数就man一下吧
client:
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
int main(int argc,char *argv[])
{
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber,nbytes;
if(3!=argc){
fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]);
exit(1);
}
if(NULL==(host=gethostbyname(argv[1]))){
fprintf(stderr,"Hostname error:%s\a\n",strerror(errno));
exit(1);
}
if(1==(sockfd=socket(AF_INET,SOCK_STREAM,0))){
fprintf(stderr,"Socket error:%s\a\n",strerror(errno));
exit(1);
}
printf("Socket id is %d\n",sockfd);
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnumber);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&server_addr,sizeof(struct sockaddr_in));
//bind sockfd
if(-1==bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))){
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit(1);
}
printf("Bind\n");
if(-1==connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))){
fprintf(stderr,"Connect Error:%s\n\a",strerror(errno));
exit(1);
}
printf("Connect\n");
if(-1==(nbytes=recv(sockfd,buffer,1024,0))){
fprintf(stderr,"Read Error:%s\n\a",strerror(errno));
exit(1);
}
buffer[nbytes]='\0';
printf("I have received:%s\n",buffer);
close(sockfd);
exit(0);
}
server:
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
int main(int argc,char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size,portnumber;
const char hello[]="Hello\n";
if(2!=argc){
fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
exit(1);
}
if((portnumber=atoi(argv[1]))<0){
fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
exit(1);
}
//server creating
if(1==(sockfd=socket(AF_INET,SOCK_STREAM,0))){
fprintf(stderr,"Socket error:%s portnumber\a\n",strerror(errno));
exit(1);
}
printf("Socket id is %d\n",sockfd);
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(portnumber);
//bind sockfd
if(-1==bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))){
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit(1);
}
printf("Bind\n");
//listen sockfd
if(-1==(listen(sockfd,5))){
fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
exit(1);
}
printf("Listen\n");
while(1){
//try until connect
sin_size=sizeof(struct sockaddr_in);
if(-1==(new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))){
fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
exit(1);
}
printf("Accept!\n");
fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));
if(-1==send(new_fd,hello,strlen(hello),0)){
fprintf(stderr,"Write Error:%s\n",strerror(errno));
exit(1);
}
close(new_fd);
}
close(sockfd);
exit(0);
}
client:
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
int main(int argc,char *argv[])
{
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber,nbytes;
if(3!=argc){
fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]);
exit(1);
}
if(NULL==(host=gethostbyname(argv[1]))){
fprintf(stderr,"Hostname error:%s\a\n",strerror(errno));
exit(1);
}
if(1==(sockfd=socket(AF_INET,SOCK_STREAM,0))){
fprintf(stderr,"Socket error:%s\a\n",strerror(errno));
exit(1);
}
printf("Socket id is %d\n",sockfd);
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnumber);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&server_addr,sizeof(struct sockaddr_in));
//bind sockfd
if(-1==bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))){
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit(1);
}
printf("Bind\n");
if(-1==connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))){
fprintf(stderr,"Connect Error:%s\n\a",strerror(errno));
exit(1);
}
printf("Connect\n");
if(-1==(nbytes=recv(sockfd,buffer,1024,0))){
fprintf(stderr,"Read Error:%s\n\a",strerror(errno));
exit(1);
}
buffer[nbytes]='\0';
printf("I have received:%s\n",buffer);
close(sockfd);
exit(0);
}
server:
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
int main(int argc,char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size,portnumber;
const char hello[]="Hello\n";
if(2!=argc){
fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
exit(1);
}
if((portnumber=atoi(argv[1]))<0){
fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
exit(1);
}
//server creating
if(1==(sockfd=socket(AF_INET,SOCK_STREAM,0))){
fprintf(stderr,"Socket error:%s portnumber\a\n",strerror(errno));
exit(1);
}
printf("Socket id is %d\n",sockfd);
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(portnumber);
//bind sockfd
if(-1==bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))){
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit(1);
}
printf("Bind\n");
//listen sockfd
if(-1==(listen(sockfd,5))){
fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
exit(1);
}
printf("Listen\n");
while(1){
//try until connect
sin_size=sizeof(struct sockaddr_in);
if(-1==(new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))){
fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
exit(1);
}
printf("Accept!\n");
fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));
if(-1==send(new_fd,hello,strlen(hello),0)){
fprintf(stderr,"Write Error:%s\n",strerror(errno));
exit(1);
}
close(new_fd);
}
close(sockfd);
exit(0);
}
追问
一定要在Linux上才能运行吗,我在windows7的VC上运行不了有错误,大概是不包含某个头文件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我这有。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询