Linux Learning Notes (V): network programming, QT signals and slots mechanism

Outbreaks of disease but can not stop the pace of our study, the current epidemic better, the house nearly a month out of their own can finally get out of wearing a mask! ! !
Here Insert Picture Description

Connect
Linux Learning Notes (a)
Linux study notes (b)
Linux Learning Notes (C): function, file IO and thread
Linux Learning Notes (d): the amount of information, synchronization, mutual exclusion and network programming

review

P V Operation Operation

The following mainly on the server and client review Description:

1, the server: server

//建立连接1socket( );//创建套接字
	声明:int socket(int domain, int type,int protocol)
	int sock = socket(PF_INET,SOCK_STREAM,0;2bind( );//绑定自己的IP
	声明:int bind(int sockfd, struct sockaddr *my_addr, int addrlen)
	bind(sock,(struct sockaddr*)(&myaddr),sizeof(myaddr));
		sockfd:是由socket调用返回的文件描述符.
		addrlen:是sockaddr结构的长度.
		my_addr:是一个指向sockaddr的指针. 在中有 sockaddr的定义
(3listen( );//监听是否有客户端向服务器发起连接
	声明:int listen(int sockfd,int backlog)
		sockfd:是bind后的文件描述符.
		backlog:设置请求排队的最大长度.当有多个客户端程序和服务端相连时, 使用这个表示可以介绍的排队长度. 函数将bind的文件描述符变为监听套接字.返回的情况和bind一样.4accept( );//接受客户端的请求
	声明:int accept(int sockfd, struct sockaddr *addr,int *addrlen)
		sockfd:是listen后的文件描述符.
		addr,addrlen是用来给客户端的程序填写的,服务器端只要传递指针就可了. bind,listen和accept是服务器端用的函数, accept调用时,服务器端的程序会一直阻塞到有一个 客户程序发出了连接. accept成功时返回最后的服务器端的文件描述符, 这个时候服务器端可以向该描述符写信息了. 失败时返回-1//接收数据5write( );//send( );6read( );//recv( );
//关闭7close( );

2, client: client

//建立连接1socket( );2connect( );//主动发起与服务器的连接
	声明:int connect(int sockfd, struct sockaddr * serv_addr,int addrlen)
		sockfd:socket返回的文件描述符.
		serv_addr:储存了服务器端的连接信息.其中sin_add是服务端的地址
		addrlen:serv_addr的长度
		connect函数是客户端用来同服务端连接的.成功时返回0,sockfd是同服务端通讯的文件描述符 失败时返回-1.
//接收数据3read( );//recv();4write( );//send();
//关闭 5close( );

(1) Network Programming:

What is network programming it? ? ? ? ?
That is, the two processes, across the computer, They need to communicate, the need butt through the network. This is the role of the socket. Analogy, two processes on two computers, you need to have a process to do the passive side, called the server. Another initiative to do the party, called the client. They are located on a computer, called the host host, have their own ip address on the network. There can be multiple processes on a computer as a server, but only one ip per machine, so distinguished by different port numbers. Therefore, the server program needs to bind to a port number on the machine. The client needs to declare that the port which address their own connections. The two processes establish communication channels through the network, and then you can send and receive information through a number of recv send, complete communication. So socket refers to the identification of system resources on behalf of the bearer of this communication.

Transfer from plain words: https: //www.zhihu.com/question/29637351/answer/110219546

Client / server mode TCP / IP network applications, communication between two processes is the main mode of interactionClient / server (Client / Server, C / S) mode, I.e., the client issuing a service request to the server, the server receives the request, provide the corresponding service.
Build client / server modelBased on the following two points:
(1) First, to establish the cause of a network of hardware and software resources, unequal computing power and information networks, the need to share, thus creating a host has many resources to provide services, fewer resources service client requests that non peer role.
(2) Secondly, the process of inter-network communication is completely asynchronous, there is neither a parent-child relationship between processes communicate with each other, not shared memory buffer, so the need for a mechanism for inter-process communication hope to establish contacts, in order to both provide synchronous data exchange, which is based on a client / server TCP / IP.
Here Insert Picture Description
Service-Terminal: The process is first started first server-side, and provide services upon request:
(1) open a communication channel and inform the local host, in particular it is willing to address recognized a port (such as FTP port 21 may be) receiving a client request;
(2) waiting for a client request arrives to the port;
(3) receiving a service request from a client, process the request and sends a response signal. Received concurrent service request to activate a new process to handle customer requests (such as UNIX systems using fork, exec). This new process to handle customer requests, does not need to respond to other requests. After the service is complete, close the new process and customer communication link, and terminates.
(4) return (2) step, the client waits for another first request.
(5) off the server

Server:
      int sock = socket(PF_INET,SOCK_STREAM,0);
      bind(sock,(struct sockaddr*)(&myaddr),sizeof(myaddr));
      struct sockaddr_in myaddr;
      myaddr.sin_family = PF_INET;
      myaddr.sin_port = htons(8888);
      myaddr.sin_addr.s_addr = inet_addr("192.168.13.119");
      listen(sock,7);
      int connfd = accept(sock,NULL,NULL);

Client:
(1) open a communication channel, and is connected to the host server is the particular port;
(2) request packet to the server sends the service, waits and receives a response; continue with the request;
close the communication channel after the end of (3) to request and terminate .

client:
      connect(sock,(struct sockaddr*)(&seraddr),sizeof(seraddr));
      send(sock,”hello”,10,0);
      recv(sock,buf,sizeof(buf),0);
      close(sock);

1, the server server receiving and transmitting data

创建套接字:Int sock = socket(PF_INET,SOCK_STREAM,0);
绑定自己的IP:bind(sock,(struct sockaddr*)(&myaddr),sizeof(myaddr));
主动发起与服务器的连接:struct sockaddr_in myaddr;
                      myaddr.sin_family = PF_INET;
                      myaddr.sin_port = htons(8888);
                      myaddr.sin_addr.s_addr = inet_addr("192.168.13.119");
监听:listen(sock,7);
接受请求:int connfd = accept(sock,NULL,NULL);
发送数据:send(sock,”hello”,10,0);
接收数据:recv(sock,buf,sizeof(buf),0);

2, the client and the client receives data transmitted

发送数据:send(sock,”hello”,10,0);
接收数据:recv(sock,buf,sizeof(buf),0);

(2) QT study program

1, How to Create a Project

Of particular note is to install the linux QT environment configuration to be added is
1, execute the command in the [terminal]: sudo gedit /etc/profile
2, add the following:

For the 32-bit system configuration:

export QTDIR=/wyj/Qt5.4.1/5.4
export PATH=$QTDIR/gcc/bin:$PATH
export LD_LIBRARY_PATH=$QTDIR/gcc/lib:$LD_LIBRARY_PATH

For 64-bit system configuration:

export QTDIR=/opt/Qt5.4.1/5.4
export PATH=$QTDIR/gcc_64/bin:$PATH
export LD_LIBRARY_PATH=$QTDIR/gcc_64/lib:$LD_LIBRARY_PATH

(1) Open Qt Creator, click New Project, create a new project: Application-> Qt Widgets Appliccation;
(2) the project name and path to create your own settings;
(3) Kit Selection Select gcc cross-compiler;
(4) if the system when openGL library does not own, do interface with 3D effects will complain, manually install openGL. First, modify system settings and software updates ---- ---- ---- China's downloaded from the server.

2, how to use the function to create a key carrying QT contact groove, text display

Signals and slots mechanism is equivalent to the callback function. In this mechanism, programmers have the opportunity to deal with two events:

  1. Pretreatment before launch signal capture event (the event does not meet the expectations can not transmit signals)
  2. The main processing performed in the slot function
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpSocket>
#include <QMovie>
#include <QLineEdit>
#include "JasonQt/JasonQt_Vop.h"

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
	// 连接完成函数
    void connectDone();

    void recvdo();
	// 按钮的槽函数
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_6_clicked();

    void on_pushButton_7_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_4_pressed();

    void on_pushButton_4_released();

private:
	// tcp连接函数
    void tcpConnect();
    bool Data_parsing(QByteArray);
private:
    Ui::Widget *ui;
    QTcpSocket tcpSocket,pic_socket;
    QMovie *movie;
    QLineEdit *line;
    JasonQt_Vop::BaiduVop m_baiduVop;
    unsigned int piclen;
    char picbuf[1024 * 1024 - 4];
};

#endif // WIDGET_H

2, create buttons and text how to use UI auto-manual

In mainwindow.ui interface, add a pushbutton button, as follows
Here Insert Picture Description
after adding textEdit control is used to display the contents of the file to be selected in the new interface, you can select the layout after the layout selection (click on a blank part of the window, and then select the layout top of the screen).

(3) Case Study

Network programming: communication between client and server

Under ubuntu use ifconfig to view the IP address of the current network.
Open two terminals, each running server and client code:

server:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include <arpa/inet.h>
#include<unistd.h>

int main(){
    /*1.创建套i接字*/
   int sock = socket(PF_INET,SOCK_STREAM,0);
    if(sock < 0){
        printf("socket error\n");
        return -1;
    }
    printf("socket success\n");
    /*2.绑定本地IP*/
    struct sockaddr_in myaddr;
    myaddr.sin_family = PF_INET;
    myaddr.sin_port = htons(8888);// 设置端口号,我这里设置的是8888
    myaddr.sin_addr.s_addr = inet_addr("xxx.xxx.xx.xxx");// 设置自己当前网端下的IP地址

    if(0 > bind(sock,(struct sockaddr*)(&myaddr),sizeof(myaddr))){
        printf("bind error\n");
        return -1;
    }
    printf("bind success\n");
    /*3.监听*/
    if(0 > listen(sock,10)){
        printf("listen error\n");
        return -1;
    }
    printf("listen success\n");
    /*4.接受链接请求*/
    int connfd = accept(sock,NULL,NULL);
    if(connfd < 0){
        printf("accept error\n");
        return -1;
    }
    printf("accept success\n");
    /*5.数据收发*/
    //send()/recv();
    char buf[20];
    gets(buf);
    int ret = send(connfd,buf,sizeof(buf),0);
    if(ret < 0){
        printf("send error\n");
        return -1;
    }
    printf("send success\n");
    /*6.关闭套接字*/
    close(connfd);
    close(sock);
return 0;
}

Client:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include <arpa/inet.h>
#include<unistd.h>

int main(){
    /*1.创建套接字*/
   int sock = socket(PF_INET,SOCK_STREAM,0);
   if(sock < 0){
        printf("sock error\n");
        return -1;
    }
    printf("sock success\n");
    /*2.主动发起与服务器的连接*/
    struct sockaddr_in seraddr;
    seraddr.sin_family = PF_INET;
    seraddr.sin_port = htons(8888);
    seraddr.sin_addr.s_addr = inet_addr("xxx.xxx.xx.xxx");// 一定与服务器的IP地址一样
    if(0 > connect(sock,(struct sockaddr*)(&seraddr),sizeof(seraddr))){
        printf("connect errorr\n");
        return -1;
    }
    printf("connect success\n");

    /*3.数据收发*/
    char buf[20];
   int ret = recv(sock,buf,sizeof(buf),0);
    if(ret < 0){
        printf("recv error\n");
        return -1;
    }
    printf("recv:%s\n",buf);
    /*4.关闭套接字*/
    close(sock);
    return 0;
}

operation result:

Here Insert Picture Description
Visible communication between the server and the client has paid off! ! ! ! ! ! Spring
Here Insert Picture Description

Everyone's trifecta is the greatest affirmation to me! ! ! It does not mean your mouse or finger. . . . . .

Continued in. . . . . .

Published 11 original articles · won praise 11 · views 515

Guess you like

Origin blog.csdn.net/ywsydwsbn/article/details/105007111