- 论坛徽章:
- 0
|
服务器端:
- #include <sys/socket.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include<sys/wait.h>
- #include <unistd.h>
- #include <stdarg.h>
- #define IPADDR "200.1.1.17"
- #define IPPORT 10001
- #define MaxCon 100
- #define MaxLen 512
- void vtcp_log(char * fmt , ...)
- {
- char filename[100];
- FILE *fp;
- va_list va;
- memset(filename,0x00,sizeof(filename));
- sprintf(filename,"%s/%s",getenv("LOGDIR"),"server.log");
- fp=fopen(filename,"a");
- va_start(va,fmt);
- vfprintf(fp,fmt,va);
- fclose(fp);
- va_end(va);
- }
- int main(int argc,char **argv)
- {
- char hostname[256];
- struct hostent * hostinfo;
- char *host,**names,**addrs;
- struct sockaddr_in srv_addr,cli_addr;
- int sock_id,cli_sock_id,len;
- int forkpid;
- char buf[512];
- int buflen;
- memset(&srv_addr,0x00,sizeof(struct sockaddr_in));
- srv_addr.sin_family=AF_INET;
- srv_addr.sin_addr.s_addr=inet_addr(IPADDR);
- srv_addr.sin_port=htons(IPPORT);
-
- sock_id=socket(AF_INET,SOCK_STREAM,0);
- if (sock_id==-1){
- vtcp_log("socket error \n");
- return 1;
- }
- if (bind(sock_id,(struct sockaddr *)&srv_addr,sizeof(srv_addr))<0){
- vtcp_log("bind error \n");
- return 1;
- }
- if (listen(sock_id,5)<0){
- vtcp_log("listen error !!!\n");
- return 1;
- }
- while(1){
- memset(&cli_addr,0x00,sizeof(struct sockaddr_in));
- len=sizeof(cli_addr);
- cli_sock_id=accept(sock_id,(struct sockaddr *)&cli_addr,(socklen_t *)&(len));
- if (cli_sock_id<0){
- vtcp_log("accept error \n");
- continue;
- } else {
- vtcp_log("cli_addr-->addr[%d]\n",cli_addr.sin_addr);
- vtcp_log("cli_addr-->port[%d]\n",cli_addr.sin_port);
- }
- if (fork()==0){
- close(sock_id);
- memset(buf,0x00,sizeof(buf));
- buflen=read(cli_sock_id,buf,MaxLen);
- if (buflen==-1){
- vtcp_log("recv error \n");
- return 0;
- } else if (buflen>0) {
- vtcp_log("databuf is [%s]\n",buf);
- } else {
- break;
- }
- memset(buf,0x00,sizeof(buf));
- strcpy(buf,"this is a test one");
- write(cli_sock_id,buf,MaxLen);
- memset(buf,0x00,sizeof(buf));
- strcpy(buf,"this is a test two");
- write(cli_sock_id,buf,MaxLen);
- close(cli_sock_id);
- exit(0);
- } else {
- close(cli_sock_id);
- }
- }
- }
复制代码
客户端
- #include<stdio.h>
- #include<stdlib.h>
- #include<errno.h>
- #include<string.h>
- #include<netdb.h>
- #include<sys/types.h>
- #include<netinet/in.h>
- #include<sys/socket.h>
- #define SERVPORT 10001
- #define MAXDATASIZE 100;
- main()
- {
- int sockfd,sendbytes;
- char buf[100];
- struct sockaddr_in serv_addr;
- if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
- {
- perror("create socket Error!");
- exit(1);
- }
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(SERVPORT);
- serv_addr.sin_addr.s_addr = inet_addr("200.1.1.17");
- bzero(&(serv_addr.sin_zero),8);
- if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)
- {
- perror("connect Error!");
- exit(1);
- }
- memset(buf,0x00,sizeof(buf));
- strcpy(buf,"cli 1");
- write(sockfd,buf,sizeof(buf));
- memset(buf,0x00,sizeof(buf));
- strcpy(buf,"cli 2");
- write(sockfd,buf,sizeof(buf));
- memset(buf,0x00,sizeof(buf));
- read(sockfd,buf,512);
- printf("%s\n",buf);
- close(sockfd);
- exit (0);
- }
复制代码
运行的时候出错:
accept的时候没有报错,但是read的时候出错!!
这是log里面的内容
cli_addr-->addr[0]
cli_addr-->port[45037]
cli_sock_id is [4]
recv error
[ 本帖最后由 zzw3206 于 2006-7-30 18:52 编辑 ] |
|