免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2322 | 回复: 6
打印 上一主题 下一主题

如何编写SOCKET的server程序? 急!!在线等 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-07 19:26 |只看该作者 |倒序浏览
我首先建立套接字,帮定端口,建立监听,然后应该如何接受请求和给出返回?

论坛徽章:
0
2 [报告]
发表于 2005-11-07 19:34 |只看该作者
在这里等别人给你讲还不如找一篇基础文章静下心来看一遍
google搜一下很多关于基础socket编程的文章的

论坛徽章:
0
3 [报告]
发表于 2005-11-07 21:33 |只看该作者
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>

#define SERVPORT 4444        /*服务器监听端口号 */
#define BACKLOG 10                /* 最大同时连接请求数 */

void GetSystemTime();

int buf[256];
char recvbuf[256];
char timebuf[64];

main(void)
{
        int recvbytes;
        int sockfd,client_fd; /*sock_fd:监听socket;client_fd:数据传输socket */
        struct sockaddr_in local_addr; /* 本机地址信息 */
        struct sockaddr_in remote_addr; /* 客户端地址信息 */
        if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
                perror("socket创建出错!";
                exit(1);
        }

        local_addr.sin_family=AF_INET;
        local_addr.sin_port=htons(SERVPORT);
        local_addr.sin_addr.s_addr = INADDR_ANY;
        bzero(&(local_addr.sin_zero),;

        if (bind(sockfd, (struct sockaddr *)&local_addr, sizeof(struct sockaddr))== -1)
        {
                perror("bind出错!";
                exit(1);
        }
        if (listen(sockfd, BACKLOG) == -1)
        {
                perror("listen出错!";
                exit(1);
        }

        while(1)
        {
                 
                int sin_size = sizeof(struct sockaddr_in);
                if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1)
                {
                        perror("accept出错";
                        continue;
                }
                printf("来自: %s 的连接n", inet_ntoa(remote_addr.sin_addr));                

                if (!fork())                 /* 子进程代码段 */
                {  
                        while(1)
                        {
                                GetSystemTime();
                                if (send(client_fd, timebuf,256,0) == -1)
                                {
                                        perror("send出错!";
                                        close(client_fd);
                                        exit(0);
                                }
                                sleep(5);
/*
                                if(recv(client_fd,recvbuf,256,0)<=0)
                                {
                                        printf("%s已断开n",inet_ntoa(remote_addr.sin_addr));
                                        close(client_fd);
                                        exit(0);
                                }
*/
                        }

                       
                        close(client_fd);
                }

                recv(client_fd, recvbuf, 256, 0);
//                printf("%sn", recvbuf);

        }
        close(sockfd);
}

void GetSystemTime()
{
        time_t timep;
        struct tm *p;
        time(&timep);
        p=localtime(&timep);

        sprintf(timebuf,"%4d%2d%2d%2d%2d%2d",
                (p->tm_year+1900),(p->tm_mon+1),p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
}

这是我写的,关于系统对时的问题

论坛徽章:
0
4 [报告]
发表于 2005-11-07 21:50 |只看该作者
原帖由 xujg 于 2005-11-7 19:34 发表
在这里等别人给你讲还不如找一篇基础文章静下心来看一遍
google搜一下很多关于基础socket编程的文章的


这才是好方法!

论坛徽章:
0
5 [报告]
发表于 2005-11-07 23:07 |只看该作者
搜索 Beej's Guide to Network Programming

论坛徽章:
0
6 [报告]
发表于 2008-11-15 00:48 |只看该作者

程序编写

将字符数组a2中的全部字符拷贝到字符数组a1中,不用strcpy函数,拷贝时也要把‘、0’拷贝过去,‘、0’后面的就不用拷贝了

[ 本帖最后由 okmijnuhbygv 于 2008-11-15 01:07 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2008-11-15 12:40 |只看该作者

操作系统原理与编程书本上的代码 机械工业出版社

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

int port = 8000;

void main() {
  struct sockaddr_in serveraddr;
  struct sockaddr_in clientaddr;
  int sockfd,temp_sockfd;
  int clientaddr_size;
  char buf[16384];
  time_t ticks;
  pid_t pid;

  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("call to socket");
    exit(1);
  }
  bzero(&serveraddr, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr = INADDR_ANY;
  serveraddr.sin_port = htons(port);

  if (bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == -1) {
    perror("call to bind");
    exit(1);
  }
  if (listen(sockfd, 20) == -1) {
    perror("call to listen");
    exit(1);
  }
  printf("Accepting connections ...\n");
  
  while(1) {
    temp_sockfd =accept(sockfd, (struct sockaddr *)&clientaddr,&clientaddr_size);
    if ((pid = fork())== 0) {/* child process the request */
      close(sockfd);   
      if (read(temp_sockfd, buf, 16384) == -1) {
      perror("call to read");
      exit(1);
      }
      printf("received from client:%s\n", buf);
      ticks = time(NULL);
      sprintf(buf,"%s\r\n",ctime(&ticks));

      if (write(temp_sockfd, buf,strlen(buf)) == -1) {
      perror("call to write");
      exit(1);
      }
     close(temp_sockfd);
     exit(0);
   }
   close(temp_sockfd);
}
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP