SHOW:
|
|
- or go back to the newest paste.
| 1 | QTClient.cpp | |
| 2 | #include <QCoreApplication> | |
| 3 | #include <QTcpSocket> | |
| 4 | int main(int argc, char *argv[]) | |
| 5 | {
| |
| 6 | QCoreApplication a(argc, argv); | |
| 7 | QTcpSocket s; | |
| 8 | s.connectToHost("127.0.0.1",12345,QIODevice::ReadWrite);
| |
| 9 | if (s.waitForConnected()) {
| |
| 10 | QByteArray arr; | |
| 11 | arr.append("Hello");
| |
| 12 | s.write(arr); | |
| 13 | s.waitForBytesWritten(6000); | |
| 14 | s.waitForReadyRead(6000); | |
| 15 | QByteArray dd = s.read(500); | |
| 16 | qDebug() << QString(dd); | |
| 17 | } | |
| 18 | return a.exec(); | |
| 19 | } | |
| 20 | ||
| 21 | zmqServer.c | |
| 22 | ||
| 23 | #include <zmq.h> | |
| 24 | #include <assert.h> | |
| 25 | #include <string.h> | |
| 26 | int main (void) | |
| 27 | {
| |
| 28 | char id[5]; | |
| 29 | char msg [256]; | |
| 30 | void *context = zmq_ctx_new (); | |
| 31 | void *sock = zmq_socket (context, ZMQ_STREAM ); | |
| 32 | int rc =zmq_bind (sock, "tcp://127.0.0.1:12345"); | |
| 33 | assert(rc==0); | |
| 34 | ||
| 35 | ||
| 36 | int size = zmq_recv(sock,&id,sizeof(id), 0 ); | |
| 37 | assert(size == 5); | |
| 38 | size = zmq_recv(sock,&msg,sizeof(msg), 0 ); | |
| 39 | assert(size==5); | |
| 40 | assert(memcmp(msg,"Hello",5) == 0); | |
| 41 | printf("%s\n",msg);
| |
| 42 | if (size != -1) {
| |
| 43 | strcpy(msg,"World!"); | |
| 44 | rc = zmq_send(sock,id,5,ZMQ_SNDMORE); | |
| 45 | assert(rc == 5); | |
| 46 | rc = zmq_send(sock,msg,strlen(msg),0); | |
| 47 | assert(rc == 6); | |
| 48 | } | |
| 49 | zmq_close (sock); | |
| 50 | zmq_ctx_destroy (context); | |
| 51 | return 0; | |
| 52 | } |