Linux网络编程10——使用UDP实现五子棋对战

思路

1. 通信

为了同步双方的棋盘,每当一方在棋盘上落子之后,都需要发送给对方一个msg消息,让对方知道落子位置。msg结构体如下:

/* 用于发给对方的信息 */
typedef struct tag_msg
{
    int msg_type;   /* 悔棋? */
    int msg_color;
    int msg_row;
    int msg_col;
}MSG, *pMSG;

2. 悔棋

用链表头插法来模拟栈,链表记录了双方下子的轨迹。结构如下:

/* 记录每一步的轨迹 */
typedef struct tag_trace
{
    int tr_cow;
    int tr_col;
    char tr_before[4]; /* 记录原来该位置的内容。注意要存下之前的!! */
    struct tag_trace* tr_next;
}TRACE, *pTRACE;

使用两个链表,一个链表用于记录自己下棋的轨迹,便于自己悔棋;另一个链表用于记录对方下棋的轨迹,便于对方悔棋。

3. 判断胜负

以水平方向为例:需要判断该棋子(包括该子)左边有多少相同颜色的棋子相连,以及右边有多少相同颜色的棋子相连,两者相加如果和大于等于5,则判赢。其余3个方向类似。具体可以参加代码。

4. 本代码用到了Linux网络编程9——对TCP与UDP的简易封装2.0中的动态库文件。

代码

chess.h

#ifndef __MY_CHESS_H__
#define __MY_CHESS_H__
#include "my_socket.h"
#include <pthread.h>

#define MSG_NORMAL 1
#define MSG_BACK  2
#define CH_BLACK 1
#define CH_WHITE 2
#define ROW 19
#define COL 19
#define WHITE_CHESS    "○"
#define BLACK_CHESS     "●"

typedef struct tag_msg
{
    int msg_type ;
    int msg_color ;
    int msg_row ;
    int msg_col ;
}MSG, *pMSG ;

typedef struct tag_trace
{
    int tr_row ;
    int tr_col ;
    char tr_before[4] ;
    struct tag_trace* tr_next ;
}TRACE, *pTRACE;

void chess_show(char ch[][COL][4], int row);
int chess_win(char arr[][COL][4], int row, int pos_x, int pos_y, char* color);

#endif

chess.c

/*************************************************************************
    > File Name: chess.c
    > Author: KrisChou
    > Mail:[email protected]
    > Created Time: Wed 03 Sep 2014 09:35:57 PM CST
 ************************************************************************/
#include "chess.h"

void chess_show(char arr[][COL][4], int row_cnt)
{
    int row, col  ;
    printf("   ");
    for(col = 0; col < COL ; col ++)
    {
        printf("%2d", col + 1);
    }
    printf("\n");
    for(row = 0; row < row_cnt ; row ++)
    {
        printf("%3d ", row + 1);
        for(col = 0; col < COL ; col ++)
        {
            printf("%s",  arr[row][col]);
            if(col != row_cnt  -1 )
            {
                printf("-");
            }
        }
        printf("\n");
    }
    printf("\n");

}
int chess_win(char arr[][COL][4], int row, int pos_x, int pos_y, char* color)
{
    // level
    int cnt1, cnt2 ;
    int index_x, index_y ;
    for(cnt1 = 0,index_x = pos_x, index_y = pos_y; index_y < COL; index_y ++)
    {
        if(strcmp(arr[index_x][index_y], color) == 0)
        {
            cnt1 ++ ;
        }else
        {
            break ;
        }
    }
    for(cnt2 = 0, index_x = pos_x , index_y = pos_y - 1 ; index_y >= 0; index_y --)
    {
        if(strcmp(arr[index_x][index_y], color) == 0)
        {
            cnt2 ++ ;
        }else
        {
            break ;
        }
    }
    if(cnt1 + cnt2 >= 5)
    {
        return 1 ;
    }
    // vertical
    for(cnt1 = 0,index_x = pos_x, index_y = pos_y; index_x >= 0; index_x --)
    {
        if(strcmp(arr[index_x][index_y], color) == 0)
        {
            cnt1 ++ ;
        }else
        {
            break ;
        }
    }
    for(cnt2 = 0, index_x = pos_x + 1 , index_y = pos_y  ; index_x < row ; index_x ++)
    {
        if(strcmp(arr[index_x][index_y], color) == 0)
        {
            cnt2 ++ ;
        }else
        {
            break ;
        }
    }
    if(cnt1 + cnt2 >= 5)
    {
        return 1 ;
    }
    // + ==
    int sum = pos_x + pos_y ;
    for(cnt1 = 0, index_x = pos_x; index_x >= 0 && sum - index_x < COL; index_x --)
    {
        if(strcmp(arr[index_x][sum - index_x], color) == 0)
        {
            cnt1 ++ ;
        }else
        {
            break ;
        }
    }
    for(cnt2 = 0, index_x = pos_x + 1; index_x < row && index_x <= sum ; index_x ++ )
    {
        if(strcmp(arr[index_x][sum - index_x], color) == 0)
        {
            cnt2 ++ ;
        }else
        {
            break ;
        }
    }
    if(cnt1 + cnt2 >= 5)
    {
        return 1 ;
    }
    // abs - ==
    int delt ;
    if(pos_x > pos_y)
    {
        delt = pos_x - pos_y ;
        for(cnt1 = 0 , index_x = pos_x; index_x >=0 && index_x >= delt; index_x --)
        {
            if(strcmp(arr[index_x][index_x - delt], color) == 0)
            {
                cnt1 ++ ;
            }else
            {
                break ;
            }
        }
        for(cnt2 = 0, index_x = pos_x + 1; index_x < row ; index_x ++)
        {
            if(strcmp(arr[index_x][index_x - delt], color) == 0)
            {
                cnt2 ++ ;
            }else
            {
                break ;
            }
        }
    }else// pos_y >= pos_x
    {
        delt = pos_y - pos_x ;
        for(cnt1 = 0 , index_x = pos_x; index_x >=0 ; index_x --)
        {
            if(strcmp(arr[index_x][index_x +  delt], color) == 0)
            {
                cnt1 ++ ;
            }else
            {
                break ;
            }
        }
        for(cnt2 = 0, index_x = pos_x + 1; index_x < row && index_x + delt < COL ; index_x ++)
        {
            if(strcmp(arr[index_x][index_x + delt], color) == 0)
            {
                cnt2 ++ ;
            }else
            {
                break ;
            }
        }
    }
    if(cnt1 + cnt2 >= 5)
    {
        return 1 ;
    }
    return 0;
}

main.c

/*************************************************************************
  > File Name: main.c
  > Author: KrisChou
  > Mail:[email protected]
  > Created Time: Wed 03 Sep 2014 10:04:07 PM CST
 ************************************************************************/
#include "chess.h"
#define B_IP "127.0.0.1"
#define B_PORT 8888
#define W_IP "127.0.0.1"
#define W_PORT 6666
#define POS_TRANS(pos)  (pos -1)
#define IS_OK(row, col) (  row >= 0 && col >= 0 && col <= 18 &&row <= 18 &&strcmp(my_chess[row][col], WHITE_CHESS) != 0 && strcmp(my_chess[row][col], BLACK_CHESS) != 0)
int main(int argc, char* argv[])
{
    char my_chess[ROW][COL][4] =
    {
        "┌","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┬","┐" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "├","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┼","┤" ,
        "└","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┴","┘"   

    };
    int sfd ;
    pTRACE my_tr, peer_tr, pStep, pTmp ;

    my_tr = NULL ;
    peer_tr = NULL ;
#ifdef FIRST
    my_socket(&sfd, MY_UDP, B_IP, B_PORT);
#else
    my_socket(&sfd, MY_UDP, W_IP, W_PORT);
#endif
    int row, col ;
    MSG my_msg ;
#ifdef FIRST
    chess_show(my_chess, ROW);
    while(1)// 0, 0
    {
        do
        {
            printf(">>");
            scanf("%d%d", &row, &col);
            if(row == 0 || col == 0)
            {
                break ;
            }
        }while( !IS_OK(POS_TRANS(row), POS_TRANS(col))) ;
        if(row !=0 && col != 0)// normal
        {
            my_msg.msg_type = MSG_NORMAL ;
            my_msg.msg_color = CH_BLACK ;
            my_msg.msg_row = POS_TRANS(row) ;
            my_msg.msg_col = POS_TRANS(col) ;
            my_sendto(NULL, sfd, &my_msg, sizeof(MSG), W_IP, W_PORT);

            pStep = (pTRACE)calloc(1, sizeof(TRACE)) ;
            pStep ->tr_row = POS_TRANS(row) ;
            pStep ->tr_col = POS_TRANS(col) ;
            strcpy(pStep ->tr_before, my_chess[POS_TRANS(row)][POS_TRANS(col)]) ;

            pStep -> tr_next = my_tr ;
            my_tr = pStep ;

            strcpy(my_chess[POS_TRANS(row)][POS_TRANS(col)], BLACK_CHESS);

            system("clear");
            chess_show(my_chess, ROW);
            if(chess_win(my_chess, ROW, POS_TRANS(row), POS_TRANS(col), BLACK_CHESS))
            {
                printf("black win !");
                exit(1);
            }
        }else
        {
            if(my_tr == NULL)
            {
                continue ;
            }else
            {
                memset(&my_msg, 0, sizeof(MSG));
                my_msg.msg_type = MSG_BACK ;
                my_sendto(NULL, sfd, &my_msg, sizeof(MSG), W_IP, W_PORT);
                strcpy(my_chess[my_tr -> tr_row][my_tr -> tr_col], my_tr ->tr_before);
                system("clear");
                chess_show(my_chess, ROW);

                pTmp = my_tr ;
                my_tr = my_tr -> tr_next ;
                free(pTmp);
                pTmp = NULL ;

                /* 本方悔棋后,需要在下一次 */
                do{
                    printf(">>");
                    scanf("%d%d", &row, &col);
                }while( !IS_OK(POS_TRANS(row), POS_TRANS(col))) ;

                my_msg.msg_type = MSG_NORMAL ;
                my_msg.msg_color = CH_BLACK ;
                my_msg.msg_row = POS_TRANS(row) ;
                my_msg.msg_col = POS_TRANS(col) ;
                my_sendto(NULL, sfd, &my_msg, sizeof(MSG), W_IP, W_PORT);

                pStep = (pTRACE)calloc(1, sizeof(TRACE)) ;
                pStep ->tr_row = POS_TRANS(row) ;
                pStep ->tr_col = POS_TRANS(col) ;
                strcpy(pStep ->tr_before, my_chess[POS_TRANS(row)][POS_TRANS(col)]) ;

                pStep -> tr_next = my_tr ;
                my_tr = pStep ;

                strcpy(my_chess[POS_TRANS(row)][POS_TRANS(col)], BLACK_CHESS);

                system("clear");
                chess_show(my_chess, ROW);
                if(chess_win(my_chess, ROW, POS_TRANS(row), POS_TRANS(col), BLACK_CHESS))
                {
                    printf("black win !");
                    exit(1);
                }

            }
        }

        memset(&my_msg, 0, sizeof(MSG));
        my_recvfrom(NULL, sfd, &my_msg, sizeof(MSG), NULL, NULL);
        if(my_msg.msg_type == MSG_NORMAL)
        {
            pStep = (pTRACE)calloc(1, sizeof(TRACE)) ;
            pStep ->tr_row = my_msg.msg_row ;
            pStep ->tr_col = my_msg.msg_col ;
            strcpy(pStep ->tr_before, my_chess[my_msg.msg_row][my_msg.msg_col]) ;
            pStep -> tr_next = peer_tr;
            peer_tr = pStep ;

            strcpy(my_chess[my_msg.msg_row][my_msg.msg_col], WHITE_CHESS);
            system("clear");
            chess_show(my_chess, ROW);
            if(chess_win(my_chess, ROW, my_msg.msg_row, my_msg.msg_col, WHITE_CHESS))
            {
                printf("white win !");
                exit(1);
            }
        }else if(my_msg.msg_type == MSG_BACK)
        {
                strcpy(my_chess[peer_tr -> tr_row][peer_tr -> tr_col], peer_tr ->tr_before);
                system("clear");
                chess_show(my_chess, ROW);

                pTmp = peer_tr ;
                peer_tr = peer_tr -> tr_next ;
                free(pTmp);
                pTmp = NULL ;

                /* 对方悔棋后,本方需要再收一次 */
                memset(&my_msg, 0, sizeof(MSG));
                my_recvfrom(NULL, sfd, &my_msg, sizeof(MSG), NULL, NULL);    

                pStep = (pTRACE)calloc(1, sizeof(TRACE)) ;
                pStep ->tr_row = my_msg.msg_row ;
                pStep ->tr_col = my_msg.msg_col ;
                strcpy(pStep ->tr_before, my_chess[my_msg.msg_row][my_msg.msg_col]) ;
                pStep -> tr_next = peer_tr;
                peer_tr = pStep ;

                strcpy(my_chess[my_msg.msg_row][my_msg.msg_col], WHITE_CHESS);
                system("clear");
                chess_show(my_chess, ROW);
                if(chess_win(my_chess, ROW, my_msg.msg_row, my_msg.msg_col, WHITE_CHESS))
                {
                    printf("white win !");
                    exit(1);
                }
        }
    }
#else
    {
        chess_show(my_chess, ROW);
        while(1)
        {
            memset(&my_msg, 0, sizeof(MSG));
            my_recvfrom(NULL, sfd, &my_msg, sizeof(MSG), NULL, NULL);
            if(my_msg.msg_type == MSG_NORMAL)
            {

                pStep = (pTRACE)calloc(1, sizeof(TRACE)) ;
                pStep ->tr_row = my_msg.msg_row ;
                pStep ->tr_col = my_msg.msg_col ;
                strcpy(pStep ->tr_before, my_chess[my_msg.msg_row][my_msg.msg_col]) ;
                pStep -> tr_next = peer_tr;
                peer_tr = pStep ;

                strcpy(my_chess[my_msg.msg_row][my_msg.msg_col], BLACK_CHESS);
                system("clear");
                chess_show(my_chess, ROW);
                if(chess_win(my_chess, ROW, my_msg.msg_row, my_msg.msg_col, BLACK_CHESS))
                {
                    printf("black win !");
                    exit(1);
                }

            }else if(my_msg.msg_type ==  MSG_BACK)
            {
                strcpy(my_chess[peer_tr -> tr_row][peer_tr -> tr_col], peer_tr ->tr_before);
                system("clear");
                chess_show(my_chess, ROW);

                pTmp = peer_tr ;
                peer_tr = peer_tr -> tr_next ;
                free(pTmp);
                pTmp = NULL ;

                memset(&my_msg, 0, sizeof(MSG));
                my_recvfrom(NULL, sfd, &my_msg, sizeof(MSG), NULL, NULL);    

                pStep = (pTRACE)calloc(1, sizeof(TRACE)) ;
                pStep ->tr_row = my_msg.msg_row ;
                pStep ->tr_col = my_msg.msg_col ;
                strcpy(pStep ->tr_before, my_chess[my_msg.msg_row][my_msg.msg_col]) ;
                pStep -> tr_next = peer_tr;
                peer_tr = pStep ;

                strcpy(my_chess[my_msg.msg_row][my_msg.msg_col], BLACK_CHESS);
                system("clear");
                chess_show(my_chess, ROW);
                if(chess_win(my_chess, ROW, my_msg.msg_row, my_msg.msg_col, BLACK_CHESS))
                {
                    printf("black win !");
                    exit(1);
                }

            }

                do{
                    printf(">>");
                    scanf("%d%d", &row, &col);
                    if(row == 0 || col == 0)
                    {
                        break ;
                    }
                }while(!IS_OK(POS_TRANS(row), POS_TRANS(col))) ;
                if(row != 0 && col != 0 )//normal
                {
                    my_msg.msg_type = MSG_NORMAL ;
                    my_msg.msg_color = CH_WHITE ;
                    my_msg.msg_row = POS_TRANS(row) ;
                    my_msg.msg_col = POS_TRANS(col) ;
                    my_sendto(NULL, sfd, &my_msg, sizeof(MSG), B_IP, B_PORT);

                    pStep = (pTRACE)calloc(1, sizeof(TRACE)) ;
                    pStep ->tr_row = POS_TRANS(row) ;
                    pStep ->tr_col = POS_TRANS(col) ;
                    strcpy(pStep ->tr_before, my_chess[POS_TRANS(row)][POS_TRANS(col)]) ;

                    pStep -> tr_next = my_tr ;
                    my_tr = pStep ;

                    strcpy(my_chess[POS_TRANS(row)][POS_TRANS(col)], WHITE_CHESS);
                    system("clear");
                    chess_show(my_chess, ROW);
                    if(chess_win(my_chess, ROW, POS_TRANS(row), POS_TRANS(col), WHITE_CHESS))
                    {
                        printf("white win !");
                        exit(1);
                    }

                }else
                {
                    if(my_tr == NULL)
                    {
                        continue ;
                    }else
                    {
                        memset(&my_msg, 0, sizeof(MSG));
                        my_msg.msg_type = MSG_BACK ;
                        my_sendto(NULL, sfd, &my_msg, sizeof(MSG), B_IP, B_PORT);
                        strcpy(my_chess[my_tr -> tr_row][my_tr -> tr_col], my_tr ->tr_before);
                        system("clear");
                        chess_show(my_chess, ROW);

                        pTmp = my_tr ;
                        my_tr = my_tr -> tr_next ;
                        free(pTmp);
                        pTmp = NULL ;

                        do{
                            printf(">>");
                            scanf("%d%d", &row, &col);
                        }while(!IS_OK(POS_TRANS(row), POS_TRANS(col)) );

                    my_msg.msg_type = MSG_NORMAL ;
                    my_msg.msg_color = CH_WHITE ;
                    my_msg.msg_row = POS_TRANS(row) ;
                    my_msg.msg_col = POS_TRANS(col) ;
                    my_sendto(NULL, sfd, &my_msg, sizeof(MSG), B_IP, B_PORT);

                    pStep = (pTRACE)calloc(1, sizeof(TRACE)) ;
                    pStep ->tr_row = POS_TRANS(row) ;
                    pStep ->tr_col = POS_TRANS(col) ;
                    strcpy(pStep ->tr_before, my_chess[POS_TRANS(row)][POS_TRANS(col)]) ;

                    pStep -> tr_next = my_tr ;
                    my_tr = pStep ;

                    strcpy(my_chess[POS_TRANS(row)][POS_TRANS(col)], WHITE_CHESS);
                    system("clear");
                    chess_show(my_chess, ROW);
                    if(chess_win(my_chess, ROW, POS_TRANS(row), POS_TRANS(col), WHITE_CHESS))
                    {
                        printf("white win !");
                        exit(1);
                    }

                    }

                }
        }

    }
#endif
    return 0 ;
}

编译如下:

gcc -o black *.c -DFIRST -lmy_socket -I/home/purple/include
gcc -o white *.c -lmy_socket -I/home/purple/include
时间: 2024-11-02 23:39:09

Linux网络编程10——使用UDP实现五子棋对战的相关文章

Linux 网络编程——TCP 和 UDP 数据报格式详解

TCP 报文格式 TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议. TCP 报文段的报头有 10 个必需的字段和 1 个可选字段.报头至少为 20 字节.报头后面的数据是可选项. 1)源端口(16位) 标识发送报文的计算机端口或进程.一个 TCP 报文段必须包括源端口号,使目的主机知道应该向何处发送确认报文. 2)目的端口(16位) 标识接收报文的目的主机的端口或进程. 3) 序号(也叫序列号)(32位) 用

Linux网络编程——TCP和UDP通信

TCP协议流程图.TCP建立即时聊天 TCP即时聊天升级:服务器在客户端断开后不断开,客户端可以多次重连服务器进行即时聊天 UDP协议流程图.UDP建立即时连接 如果在已经处于 ESTABLISHED状态下的socket(一般由端口号和标志符区分)需调用closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket,但也可以直接用setsockopt和reuse. SO_RCVLOWAT设置接收缓冲区下限 1.TCP协议的流程图 服务端:socket---bi

Linux网络编程:基于UDP的程序开发回顾篇

基于无连接的UDP程序设计 同样,在开发基于UDP的应用程序时,其主要流程如下: 对于面向无连接的UDP应用程序在开发过程中服务端和客户端的操作流程基本差不多.对比面向连接的TCP程序,服务端少了listen和accept函数.前面我们也说过listen函数最主要的作用就是将一个socket套接字描述符转为被动监听模式,然后调用accept主要是用于等待客户端(用connect)来连接服务器.connect函数不仅可以用于流式套接字还可用于数据报式套接字.在TCP中,客户端调用connect函数

Linux网络编程5&mdash;&mdash;使用UDP协议实现群聊

引言 本文实现的功能类似于我之前所写的一篇博文(Linux之select系统调用_2),区别在于进程之间的通信方式有所不同.之前的文章中,我所使用的是管道,而本文我将会使用socket接口. 需求 客户端发送消息给服务器,服务器收到消息后,会转发该消息给所有客户端. 思路 1. server端维护一个链表,用于存放客户端的联系方式.结构如下: typedef struct sockaddr_in SA ; typedef struct client_tag { SA ct_addr; struc

Linux网络编程入门 (转载)

http://www.cnblogs.com/RascallySnake/archive/2012/01/04/2312564.html (一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端         网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户端        在网络程序中,如果一个程序主动和外面的程序通信,那么我们把这个程序称为客户端程序. 比如我们使用ftp程序从另外一        个地方获取文件

Linux网络编程入门

(一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍 客户端和服务端 网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户端 在网络程序中,如果一个程序主动和外面的程序通信,那么我们把这个程序称为客户端程序. 比如我们使用ftp程序从另外一 个地方获取文件的时候,是我们的ftp程序主动同外面进行通信(获取文件), 所以这个地方我们的ftp程序就是客户端程序. 服务端 和客户端相对应的程序即为服务端程序.被动的等待外面的程序来和自己通

[转] - Linux网络编程 -- 网络知识介绍

(一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端         网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户端        在网络程序中,如果一个程序主动和外面的程序通信,那么我们把这个程序称为客户端程序. 比如我们使用ftp程序从另外一        个地方获取文件的时候,是我们的ftp程序主动同外面进行通信(获取文件), 所以这个地方我们的ftp程序就是客户端程序. 服务端        和客户端相

Linux 网络编程系列教程

01.Linux网络编程1--网络协议入门 02.Linux网络编程2--无连接和面向连接协议的区别 03.Linux网络编程3--编程准备:字节序.地址转换 04.Linux网络编程4--UDP编程 05.Linux网络编程5--广播 06.Linux网络编程7--多播 08.Linux网络编程8--TCP编程 09.Linux网络编程9--并发服务器 10.Linux网络编程10--原始套接字能干什么? 11.Linux网络编程11--原始套接字编程 12.Linux网络编程12--原始套接

linux网络编程笔记——UDP

目前这部分代码会出现阻塞问题,暂时尚未解决 #include "udp.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <malloc.h> #include <sys/types.h> #include <sys/socket