linux网络编程

2023-12-18 22:47:10

传输层协议UDP和TCP区别

UDP是无连接的
UDP是不可靠的
UDP资源开销小
UDP音视频传输
面向报文

TCP是有连接的
TCP是可靠传输,ACK应答机制,超时重传
TCP资源开销大
TCP传文件
面向字节流

粘包问题
1.结束标志
2.固定大小
3.自定义协议

UDP广播 局域网 组播

协议头

TCP协议传输

?三次握手,四次挥手

?用TCP协议实现查字典功能:

//服务端代码
#include "head.h"

int main(int argc, char const *argv[])
{
    struct sockaddr_in seraddr;
    bzero(&seraddr, sizeof(seraddr));
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == sockfd)
    {
        perror("fail to socket");
        return -1;
    }

    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(50000);
    seraddr.sin_addr.s_addr = INADDR_ANY;
    
    int bret = bind(sockfd, (SA)&seraddr, sizeof(seraddr));
    if(-1 == bret)
    {
        perror("fail to bind");
        return -1;
    }
    int lret = listen(sockfd, 10);
    if(-1 == lret)
    {
        perror("fail to listen");
        return -1;
    }
    int serfd = accept(sockfd, NULL, NULL);
    if(-1 == serfd)
    {
        perror("fail to accept");
        return -1;
    }
 
    FILE *fp = fopen("dict.txt", "r");
    if(fp == NULL)
    {
        perror("fail to fopen");
        return -1;
    }

    char word[128] = {0};
    char summar[1024] = {0};
    
    while(1)
    {
        memset(word, 0, sizeof(word));
        fseek(fp, 0, SEEK_SET);
        recv(serfd, word, sizeof(word), 0);
        printf("%s\n", word);
        if(!strcmp(word, "#quit"))
        {
            break;
        }
        while(1)
        {
            memset(summar, 0, sizeof(summar));
            char *psum = summar;
            char *pret = fgets(summar, sizeof(summar), fp);
            if(!strncmp(word, summar, strlen(word)))
            {
                psum += strlen(word);
                while(*psum == ' ')
                {
                    psum++;
                }
                send(serfd, psum, strlen(psum), 0);
                break;
            }
            if(NULL == pret)
            {
                char fail[64] = "please enter a correct word, or you can quit!\n";
                send(serfd, fail, strlen(fail), 0);
                break;
            }
        }
    }

    fclose(fp);
    close(sockfd);
    close(serfd);
    return 0;
}
//客户端代码
#include "head.h"

int main(int argc, char const *argv[])
{
    struct sockaddr_in seraddr;
    bzero(&seraddr, sizeof(seraddr));
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == sockfd)
    {
        perror("fail to socket");
        return -1;
    }

    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(50000);
    seraddr.sin_addr.s_addr = inet_addr("192.168.0.185");
    int conn = connect(sockfd, (SA)&seraddr, sizeof(seraddr));
    if(-1 == conn)
    {
        perror("fail to connect");
        return -1;
    }

    char word[128] = {0};
    char mean[1024] = {0};
    while(1)
    {
        memset(word, 0, sizeof(word));
        memset(mean, 0, sizeof(mean));
        printf("enter a word:");
        scanf("%s", word);
        if(!strcmp(word, "#quit"))
        {
            break;
        }
        send(sockfd, word, strlen(word), 0);
        recv(sockfd, mean, sizeof(mean), 0);
        printf("%s", mean);
    }

    close(sockfd);
    return 0;
}
//头文件
#ifndef _HEAD_H_
#define _HEAD_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#endif

文章来源:https://blog.csdn.net/qq_58687815/article/details/135070873
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。