Chinaunix首页 | 论坛 | 博客
  • 博客访问: 309991
  • 博文数量: 55
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 615
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-07 13:47
文章分类
文章存档

2011年(1)

2010年(2)

2009年(14)

2008年(38)

我的朋友

分类: LINUX

2008-12-18 15:44:33

基于TCP的socket编程步骤:

服务器端:
1、 创建套接字;
使用socket()
#include
#include
int socket(int domain,int type,int protocol)
建立服务器端的socket套接字
2、 绑定套接字到一个IP地址和一个端口上;
使用bind()
#include
#include

int bind(int sockfd,struct sockaddr * myaddr,int addrlen);

3、 将套接字设置为监听模式,以等待连接请求;
使用listen()
#include
int listen(int sockfd,int backlog)

4、 请求到来后,接受连接请求,并返回一个与此次连接对应的套接字;
使用accept()
#include
int accept(int sockfd,struct sockaddr * addr,int * addrlen)
此时新建连接,并创建新的Socket套接字,此时addr为客户端的addr信息。

5、 用返回的套接字和客户端进行通信;
使用recv()和send()
int read(int fd,char * buf,int len)
int write(int fd,char * buf,int len)

6、 关闭当前的连接,进入等待状态,继续等待客户端的连接;
使用close()
#include
int close(int sockfd);

7、 关闭服务器端的套接字描述符;
使用close()
#include
int close(int sockfd);

客户端:
1、 创建客户端套接字;
使用socket()
#include
#include
int socket(int domain,int type,int protocol)

2、 向服务器发出连接请求;
使用connect()
#include
#include
int connect(int sockfd,struct sockaddr * servaddr,int addrlen)
其中参数servaddr指定远程服务器的套接字地址,包括服务器的IP地址和端口号
3、 和服务器端进行网络通信;
使用recv()和send()
int read(int fd,char * buf,int len)
int write(int fd,char * buf,int len)
4、 关闭套接字;
使用close()
#include
int close(int sockfd);

简单的实现代码如下:

服务器端程序:

//服务器端代码..........

#include
#include
#include
#include
#include
                                                                                                 
#define MYPORT 8000
#define BACKLOG 10
int main(void)
{
        int sockfd,con_fd,numbytes,ret,pid;
        struct sockaddr_in my_addr;
        struct sockaddr_in their_addr;
        int sin_size;
        int lis;
        char buf[256];
                                                                                                 
socketing:
        //creat socket.

        sockfd=socket(AF_INET,SOCK_STREAM,0);
        if (sockfd==-1)
        {
                printf("failed when creating\n");
                exit(-1);
        }
        //binding socket.

        my_addr.sin_family=AF_INET;
        my_addr.sin_port=htons(MYPORT);
        my_addr.sin_addr.s_addr=INADDR_ANY;
        bzero(&(my_addr.sin_zero),8);
        ret=bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr));
                                                                                                 
        if (ret==-1)
        {
                printf("failed when binding\n");
                exit(-1);
        }
        //listen.

        if (listen(sockfd,BACKLOG)==-1)
        {
                printf("failed when listening\n");
                exit(-1);
        }
        printf("server listening...wait for connect...\n");
        while (1)
        {
                sin_size=sizeof(struct sockaddr);
                con_fd=accept(sockfd,(struct sockaddr*)&their_addr,&sin_size);
                if (con_fd<0)
                {
                        printf("failed when accepting\n");
                        exit(-1);
                }
                //recv a string from client and print the string.

                if (recv(con_fd,buf,sizeof(buf),0)==-1)
                {
                        printf("failed when receiving the string\n");
                        exit(-1);
                }
                printf(" received form client...%s\n",buf);
                //send the buf string to client and print it.

                if (send(con_fd,&buf,sizeof(buf),0)==-1)
                {
                        printf("failed when sending the string");
                        exit(-1);
                }
                printf(" send to client...%s\n",buf);
                close(con_fd);
        }
                close(sockfd);
                exit(0);
}




客户端程序:

//客户端程序:


#include
#include
#include
#include
#include
                                                                                                 
#define MYPORT 8000
                                                                                                 
//local host

//char * host_name="127.0.0.1";

                                                                                                 
int main(int argc,char *argv[])
{
        int sockfd,con_fd,numbytes,ret,pid;
        struct sockaddr_in server_addr;
        struct hostent *their_addr;
        int sin_size;
        int lis;
        char buf[256];
        char *str="Default test string.\n";
        char *ip="192.192.192.190\n";
        //if you input the new host ip after the command

        //then change the default local host ip address.

        if (argc<2)
                printf("default local host : 192.192.192.190 , or you can input one after the command.\n");
        else
        {
                ip=argv[1];
                printf("local host ip = %s\n",ip);
        }
                                                                                                 
socketing:
        //creat socket.

        server_addr.sin_family=AF_INET;
        server_addr.sin_port=htons(MYPORT);
        server_addr.sin_addr.s_addr=inet_addr(ip); //inet_addr(ip);

        bzero(&(server_addr.sin_zero),8);
        sockfd=socket(AF_INET,SOCK_STREAM,0);
        if (sockfd==-10)
        {
                printf("failed when creating\n");
                exit(-1);
        }
                                                                                                 
        //connect the server.

        if (connect(sockfd,(void*)&server_addr,sizeof(server_addr))==-1)
        {
                printf("failed when connecting\n");
                exit(-1);
        }
                                                                                                 
        //send the default string to server.

        printf(" send to server...%s\n",str);
        if (send(sockfd,str,strlen(str),0)==-1)
        {
                printf("failed when sending\n");
                exit(-1);
        }
        //receive a string from server.

        if (recv(sockfd,buf,sizeof(buf),0)==-1)
        {
                printf("failed when receiving\n");
                exit(-1);
        }
                                                                                                 
        printf(" response from server...%s\n",buf);
        close(sockfd);
}

阅读(1625) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~