chat::chat(QString pasvusername, QString pasvuserip) : ui(new Ui::chat) { ui->setupUi(this); ui->textEdit->setFocusPolicy(Qt::StrongFocus); ui->textBrowser->setFocusPolicy(Qt::NoFocus); ui->textEdit->setFocus(); ui->textEdit->installEventFilter(this); a = 0; is_opened = false; // this->is_opened = false; xpasvusername = pasvusername; xpasvuserip = pasvuserip; ui->label->setText(tr("与%1聊天中 对方IP:%2").arg(xpasvusername).arg(pasvuserip)); //UDP部分 xchat = new QUdpSocket(this); xport = 45456; // xchat->bind(xport, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint); xchat->bind( QHostAddress(getIP()), xport ); connect(xchat, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams())); //TCP部分 server = new TcpServer(this); connect(server,SIGNAL(sendFileName(QString)),this,SLOT(sentFileName(QString))); connect(ui->textEdit,SIGNAL(currentCharFormatChanged(QTextCharFormat)),this,SLOT(currentFormatChanged(const QTextCharFormat))); }
chat::chat(QString pasvusername,QString pasvuserip): ui(new Ui::chat) { ui->setupUi(this); ui->textEdit->setFocusPolicy(Qt::StrongFocus); ui->textBrowser->setFocusPolicy(Qt::NoFocus); ui->textEdit->setFocus(); ui->textEdit->installEventFilter(this); ui->send->setStyleSheet(tr("QPushButton{ border:0px;border-radius:5px;color:white;background-color:rgb(172, 213, 191)}QPushButton:hover{background-color:rgb(153, 186, 255);}QPushButton:pressed{background-color:rgb(153, 186, 255);}")); //退出按钮 ui->close->setStyleSheet(tr("QPushButton{border:0px;border-radius:5px;color:white;background-color:rgb(195, 195, 195);}QPushButton:hover{background-color:#505050;}QPushButton:pressed{background-color:#505050;}")); a = 0; is_opened = false; // this->is_opened = false; xpasvusername = pasvusername; xpasvuserip = pasvuserip; ui->label->setText(tr("与%1聊天中 对方IP:%2").arg(xpasvusername).arg(pasvuserip)); //UDP部分 xchat = new QUdpSocket(this); xport=6900; // xchat->bind(xport, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint); QHostAddress chat_me(getIP()); xchat->bind( chat_me, xport); connect(xchat, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams())); //TCP部分 server = new TcpSend(this); connect(server,SIGNAL(sendFileName(QString)),this,SLOT(sentFileName(QString))); connect(ui->textEdit,SIGNAL(currentCharFormatChanged(QTextCharFormat)),this,SLOT(currentFormatChanged(const QTextCharFormat))); }
int main(int argc,char **argv) { struct sockaddr_in serv_addr; char buf[MAXLINE]; int sock_id; int read_len; int send_len; FILE *fp; int i_ret; if (argc != 4) { usage(argv[0]); } if ((fp = fopen(argv[3],"rb")) == NULL) { perror("Open file failed\n"); exit(0); } if ((sock_id = socket(AF_INET,SOCK_STREAM,0)) < 0) { perror("Create socket failed\n"); exit(0); } memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(atoi(argv[2])); //inet_pton(AF_INET, argv[1], &serv_addr.sin_addr); serv_addr.sin_addr.s_addr = inet_addr(argv[1]); /* connect the server commented by guoqingbo*/ i_ret = connect(sock_id, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)); if (-1 == i_ret) { printf("Connect socket failed\n"); return -1; } char *fname = basename(argv[3]); if (sendFileName(sock_id, fname) < 0){ perror("Send file name failed\n"); return -1; } /* transported the file commented by guoqingbo*/ bzero(buf, MAXLINE); while ((read_len = fread(buf, sizeof(char), MAXLINE, fp)) >0 ) { send_len = send(sock_id, buf, read_len, 0); if ( send_len < 0 ) { perror("Send file failed\n"); exit(0); } bzero(buf, MAXLINE); } fclose(fp); close(sock_id); printf("Send Finish\n"); return 0; }
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); this->resize(300, 550); this->move(1000, 50); ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//设置表格为整行选中的方式//设置选择模式 //ui->tableWidget->verticalHeader()->setResizeMode(QHeaderView::Stretch);//使行完全填充并平分 ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);//使列完全填充并平分 Sender = new FileSender(this); udpSocket = new QUdpSocket(this); port = 45454; isGame = 0; udpSocket->bind(port, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);//绑定UDP //ShareAddress模式:允许其他服务绑定到相同的地址和端口//ReuseAddressHint模式:重新连接服务器 //readyRead()信号是每当有新的数据来临时就被触发 connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));//进行监听是否有数据报文需要读取 connect(Sender, SIGNAL(sendFileName(QString, QString)), this, SLOT(sendFileName(QString, QString))); }
void MyFileLoadBlock::timer100msec() { if(m_attempt>5) { emit statusBlock(this,99); } else { switch(m_state) { case 0: sendFileName(); break; case 2: sendQueryData(); break; } } }
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); udpSocket = new QUdpSocket(this); port = 45454; udpSocket->bind(port, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams())); sendMessage(NewParticipant); server = new TcpServer(this); connect(server, SIGNAL(sendFileName(QString)), this, SLOT(getFileName(QString))); connect(ui->messageTextEdit, SIGNAL(currentCharFormatChanged(QTextCharFormat)), this, SLOT(currentFormatChanged(const QTextCharFormat))); }
FileDialog::FileDialog(QWidget *parent) : QWidget(parent), model(new QDirModel()), treeview(new QTreeView()), okButton(new QPushButton("OK")), pathField(new QLineEdit()), path("") { treeview->setModel(model); QObject::connect(treeview, SIGNAL(clicked(QModelIndex)), this, SLOT(getFileName(QModelIndex))); QObject::connect(okButton, SIGNAL(clicked()), this, SLOT(sendFileName())); QVBoxLayout *vlayout = new QVBoxLayout(this); this->setLayout(vlayout); vlayout->addWidget(treeview, 2); QHBoxLayout* hlayout = new QHBoxLayout(); vlayout->addLayout(hlayout, 1); hlayout->addWidget(pathField, 2); hlayout->addWidget(okButton); this->setFixedSize(600, 400); }
/** * Begins program execution * @param argc The number of command line arguments * @param argv An array of C strings containing each command line argument * @return The exit code */ int main(int argc, char** argv) { /* Check the command line arguments */ if (argc < 2) { fprintf(stderr, "USAGE: %s <FILE NAME>\n", argv[0]); exit(-1); } /* Install a signal handler for the SIGUSR2 signal */ if (signal(SIGUSR2, usr2Signal) == SIG_ERR) { perror("signal"); exit(-1); } /* Initialize */ init(shmid, sharedMemPtr); /* Get the pid of the receiver */ rpid = recvpid(); /* Send the pid of this process */ sendpid(); /* Send the name of the file */ sendFileName(argv[1]); /* Send the file */ fprintf(stderr, "The number of bytes sent is %lu\n", sendFile(argv[1])); /* Cleanup */ cleanUp(shmid, sharedMemPtr); return 0; }
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); ui->label->setText("<a href=\"http://www.yafeilinux.com\"> www.yafeilinux.com </a>"); this->resize(850,550); ui->textEdit->setFocusPolicy(Qt::StrongFocus); ui->textBrowser->setFocusPolicy(Qt::NoFocus); ui->textEdit->setFocus(); ui->textEdit->installEventFilter(this); udpSocket = new QUdpSocket(this); port = 45454; udpSocket->bind(port,QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint); connect(udpSocket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams())); sendMessage(NewParticipant); server = new TcpServer(this); connect(server,SIGNAL(sendFileName(QString)),this,SLOT(sentFileName(QString))); connect(ui->textEdit,SIGNAL(currentCharFormatChanged(QTextCharFormat)),this,SLOT(currentFormatChanged(const QTextCharFormat))); }
int main(int argc,char *argv[]) { if(argc != 3) { printf("\n\tUsage : ./a.out 127.0.0.1 mp3_file.mp3\n"); exit(1); } struct sockaddr_in servaddr; bzero(&servaddr,sizeof(servaddr)); int sockfd,connfd; sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(sockfd < 0) { printf("\n\tSocket Error\n"); perror("\n\tSocket"); exit(1); } servaddr.sin_family = AF_INET; servaddr.sin_port = htons(9999); inet_pton(sockfd,argv[1],&servaddr.sin_addr); connfd = connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); if(connfd < 0) { printf("\n\tConnect Error\n"); perror("\n\tConnect\n"); exit(1); } sendFileName(sockfd,argv[2]); sendFileData(sockfd,argv[2]); printf("\n\tExiting\n"); }