Linux下简单的socket

客服端输入信息传送给服务器,服务器对数据库进行处理

server.pc通过预编译生成.c文件

server.pc如下

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
EXEC SQL INCLUDE SQLCA;

EXEC SQL BEGIN DECLARE SECTION;
char *id_pass="nxy/nxy";
int id,money;
char name[10];
EXEC SQL END DECLARE SECTION;
struct bank{
int id;
char name[10];
int money;
}p;
char recvbuff[50],sendbuff[50];
int main(int argc, char argv[])
{
        int sockfd,new_fd;
        struct sockaddr_in server_addr;
        struct sockaddr_in client_addr;
        int sin_size,portnumber;

//      if (argc!=2)
//      {
//              fprintf(stderr,"Usage:%s portnumber a",argv[0]);
//              exit(1);
//      }
//      if ((portnumber = atoi(argv[1])) < 0)
//      {
//              fprintf(stderr,"Usage:%s portnumber a",argv[0]);
//              exit(1);
//      }
        portnumber=8888;
        if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
        {
                fprintf(stderr,"Socket error:%s a",strerror(errno));
                exit(1);
        }
        printf("Socket Create!\n");

        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);

        if (bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1)
        {
                fprintf(stderr,"Bind Error:%s a",strerror(errno));
                exit(1);
        }
        printf("Bind!\n");
        if (listen(sockfd,5) == -1)
        {
                fprintf(stderr,"Linsten error:%s a",strerror(errno));
                exit(1);
        }
        printf("Begin Listen!\n");
        while(1)
        {
                sin_size=sizeof(struct sockaddr_in);
                if ((new_fd = accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size)) == -1)
                {
                        fprintf(stderr,"Accept error:%s a",strerror(errno));
                        exit(1);
                }
                fprintf(stderr,"有用户登录:Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));
        while(1){
                bzero(recvbuff,50);
                bzero(&p,sizeof(p));
                int len;
                len=recv(new_fd,recvbuff,50,0);
                if (len > 0)
                {
                        memcpy(&p,recvbuff,sizeof(p));
                        id = p.id;
                        money = p.money;
                        if (id == 0)
                        {
                                printf("用户已退出!\n");
                                break;
                        }
                        printf("客服端输入id为:%d,输入现金为:%d\n",id,money);
                        EXEC SQL CONNECT :id_pass;
                        EXEC SQL UPDATE BANK SET money=money+:money where id=:id;
                        if (!sqlca.sqlcode)
                        {
                                printf("操作完成!\n");
                        }
                        else
                                printf("ERROR!请检查输入是否正确!\n");
                        EXEC SQL COMMIT WORK RELEASE;
                }
        }
                close(new_fd);
        }
        close(sockfd);
        exit(0);
}


client.c如下

#include<stdio.h>
#include<strings.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<netdb.h>
struct bank{
int id;
char name[10];
int money;
}p;
char sendbuff[50],recvbuff[50];
int main(int argc, char *argv[])
{
        int sockfd;
        struct sockaddr_in server_addr;
        struct hostent *host;
        int portnumber,nbytes;

        if (argc != 3)
        {
                fprintf(stderr,"Usage:%s hostname portnumbera\n ",argv[0]);
                exit(1);
        }
        if ((host = gethostbyname(argv[1])) == NULL)
        {
                fprintf(stderr,"Gethostname error\n");
                exit(1);
        }
        if ((portnumber = atoi(argv[2])) < 0)
        {
                fprintf(stderr,"Usage:%s hostname portnumbera\n ",argv[0]);
                exit(1);
        }
        if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
        {
                fprintf(stderr,"Socket Error:%s a\n",strerror(errno));
                exit(1);
        }

        bzero(&server_addr,sizeof(server_addr));
        server_addr.sin_family=AF_INET;
        server_addr.sin_port=htons(portnumber);
        server_addr.sin_addr=*((struct in_addr *)host->h_addr);

        if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1)
        {
                fprintf(stderr,"Connet Error:%s a\n",strerror(errno));
                exit(1);
        }
        while(1){
                bzero(sendbuff,50);
                bzero(&p,sizeof(p));
                printf("please input the id and money like 100 100 (exit:0 0)\n");
                scanf("%d %d",&p.id,&p.money);
                memcpy(sendbuff,&p,sizeof(p));
                int len=send(sockfd,sendbuff,sizeof(sendbuff),0);
                if (p.id == 0)
                {
                        break;
                }
                if (len < 0)
                {
                        printf("ERROR!!!\n");
                }
                else if (len > 0)
                {
                        printf("OK!\n");
                }
                }
        close(sockfd);
        exit(0);
}



阅读更多
个人分类: oracle Linux
想对作者说点什么? 我来说一句

linux下基于socket的多线程编程

2013年07月02日 2KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭