设为首页 加入收藏

TOP

C语言实现服务端和客户端进行TCP通信实例(一)
2015-07-16 12:56:59 来源: 作者: 【 】 浏览:17
Tags:语言 实现 服务 客户端 进行 TCP 通信 实例

本文给出一个很实用的C语言实现的服务端和客户端进行TCP通信的小例子。具体实现上非常简单,只是平时编写类似程序,具体步骤经常忘记,还要总是查,暂且将其记下来,方便以后参考。


(1)客户端程序,编写一个文件client.c,内容如下:


#include
#include
#include
#include
#include
#include
#include
#include ? /* netdb is necessary for struct hostent */


#define PORT 4321? /* server port */


#define MAXDATASIZE 100


int main(int argc, char *argv[])
{
? ? int sockfd, num;? ? /* files descriptors */
? ? char buf[MAXDATASIZE];? ? /* buf will store received text */
? ? struct hostent *he;? ? /* structure that will get information about remote host */
? ? struct sockaddr_in server;
? ?
? ? if (argc != 2)
? ? {
? ? ? ? printf("Usage: %s \n",argv[0]);
? ? ? ? exit(1);
? ? }
? ?
? ? if((he=gethostbyname(argv[1]))==NULL)
? ? {
? ? ? ? printf("gethostbyname() error\n");
? ? ? ? exit(1);
? ? }
? ?
? ? if((sockfd=socket(AF_INET,SOCK_STREAM, 0))==-1)
? ? {
? ? ? ? printf("socket() error\n");
? ? ? ? exit(1);
? ? }
? ? bzero(&server,sizeof(server));
? ? server.sin_family = AF_INET;
? ? server.sin_port = htons(PORT);
? ? server.sin_addr = *((struct in_addr *)he->h_addr);
? ? if(connect(sockfd, (struct sockaddr *)&server, sizeof(server))==-1)
? ? {
? ? ? ? printf("connect() error\n");
? ? ? ? exit(1);
? ? }
  
  char str[] = "horst\n"


? ? if((num=send(sockfd,str,sizeof(str),0))==-1){
? ? ? ? printf("send() error\n");
? ? ? ? exit(1);
? ? }
? ? if((num=recv(sockfd,buf,MAXDATASIZE,0))==-1)
? ? {
? ? ? ? printf("recv() error\n");
? ? ? ? exit(1);
? ? }
? ? buf[num-1]='\0';
? ? printf("server message: %s\n",buf);
? ? close(sockfd);
? ? return 0;
}


复制代码


?


(2)服务器端,编写server.c,内容如下



复制代码
#include
#include
#include
#include
#include
#include
#include
#include
#include


#define PORT 4321


#define BACKLOG 1
#define MAXRECVLEN 1024


int main(int argc, char *argv[])
{
? ? char buf[MAXRECVLEN];
? ? int listenfd, connectfd;? /* socket descriptors */
? ? struct sockaddr_in server; /* server's address information */
? ? struct sockaddr_in client; /* client's address information */
? ? socklen_t addrlen;
? ? /* Create TCP socket */
? ? if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
? ? {
? ? ? ? /* handle exception */
? ? ? ? perror("socket() error. Failed to initiate a socket");
? ? ? ? exit(1);
? ? }
?
? ? /* set socket option */
? ? int opt = SO_REUSEADDR;
? ? setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));


? ? bzero(&server, sizeof(server));


? ? server.sin_family = AF_INET;
? ? server.sin_port = htons(PORT);
? ? server.sin_addr.s_addr = htonl(INADDR_ANY);
? ? if(bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1)
? ? {
? ? ? ? /* handle exception */
? ? ? ? perror("Bind() error.");
? ? ? ? exit(1);
? ? }
? ?
? ? if(listen(listenfd, BACKLOG) == -1)
? ? {
? ? ? ? perror("listen() error. \n");
? ? ? ? exit(1);
? ? }


? ? addrlen = sizeof(client);
? ? while(1){
? ? ? ? if((connectfd=accept(listenfd,(struct sockaddr *)&client, &addrlen))==-1)
? ? ? ? ? {
? ? ? ? ? ? perror("accept() error. \n");
? ? ? ? ? ? exit(1);
? ? ? ? ? }


? ? ? ? struct timeva l tv;
? ? ? ? gettimeofday(&tv, NULL);
? ? ? ? ? printf("You got a connection from client's ip %s, port %d at time %ld.%ld\n",inet_ntoa(client.sin_addr),htons(client.sin_port), tv.tv_sec,tv.tv_usec);
? ? ? ?
? ? ? ? int iret=-1;
? ? ? ? while(1)
? ? ? ? {
? ? ? ? ? ? iret = recv(connectfd, buf, MAXRECVLEN, 0);
? ? ? ? ? ? if(iret>0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? printf("%s\n", buf);
? ? ? ? ? ? }else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? close(connectfd);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? /* print client's ip and port */
? ? ? ? ? ? send(connectfd, buf, iret, 0); /* send to the client welcome message */
? ? ? ? }
? ? }
? ? close(listenfd); /* close listenfd */
? ? return 0;
}


复制代码


?


(3)编译运行


以上

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇[C/C++基础] C语言常用函数memset.. 下一篇Java中如何实现单例模式

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: