package com.firebird.server;
import java.util.ArrayList;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;
import org.zeromq.ZMsg;
public class HwServer {
public static class HwServerRunnable implements Runnable{
@Override
public void run(){
ZContext cxt = new ZContext(5);
Socket hwServer = cxt.createSocket(ZMQ.REP);
hwServer.bind("tcp://*:5550");
while(!Thread.currentThread().isInterrupted()){
ZMsg msg = ZMsg.recvMsg(hwServer, 0);
if(msg == null){
continue;
}
System.out.println(msg.toString());
msg.destroy();
try{
Thread.sleep(1);
}
catch(InterruptedException e){
break;
}
ZMsg sendMsg = new ZMsg();
sendMsg.addString("world");
sendMsg.send(hwServer);
sendMsg.destroy();
}
hwServer.close();
cxt.destroy();
}
}
public static class HwClientRunnable implements Runnable{
@Override
public void run(){
ZContext cxt = new ZContext(1);
Socket hwSocket = cxt.createSocket(ZMQ.REQ);
hwSocket.connect("tcp://*:5550");
while(!Thread.currentThread().isInterrupted()){
ZMsg sendMsg = new ZMsg();
sendMsg.addString("hello");
sendMsg.send(hwSocket);
sendMsg.destroy();
ZMsg msg = ZMsg.recvMsg(hwSocket, 0);
if(msg == null){
continue;
}
System.out.println(msg.toString());
msg.destroy();
}
hwSocket.close();
cxt.destroy();
}
}
public static void main(String[] args) {
ArrayList<Thread> thArray = new ArrayList<Thread>(5);
for(int i = 0 ;i< 5;i++){
Thread th = new Thread(new HwClientRunnable());
thArray.add(th);
th.start();
}
Thread bossThread = new Thread(new HwServerRunnable());
bossThread.start();
/*
try{
Thread.sleep(1000);
for(int i = 0;i<thArray.size();i++){
thArray.get(i).interrupt();
}
bossThread.interrupt();
}catch(Exception e){
e.printStackTrace();
}*/
}
}
import java.util.ArrayList;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;
import org.zeromq.ZMsg;
public class HwServer {
public static class HwServerRunnable implements Runnable{
@Override
public void run(){
ZContext cxt = new ZContext(5);
Socket hwServer = cxt.createSocket(ZMQ.REP);
hwServer.bind("tcp://*:5550");
while(!Thread.currentThread().isInterrupted()){
ZMsg msg = ZMsg.recvMsg(hwServer, 0);
if(msg == null){
continue;
}
System.out.println(msg.toString());
msg.destroy();
try{
Thread.sleep(1);
}
catch(InterruptedException e){
break;
}
ZMsg sendMsg = new ZMsg();
sendMsg.addString("world");
sendMsg.send(hwServer);
sendMsg.destroy();
}
hwServer.close();
cxt.destroy();
}
}
public static class HwClientRunnable implements Runnable{
@Override
public void run(){
ZContext cxt = new ZContext(1);
Socket hwSocket = cxt.createSocket(ZMQ.REQ);
hwSocket.connect("tcp://*:5550");
while(!Thread.currentThread().isInterrupted()){
ZMsg sendMsg = new ZMsg();
sendMsg.addString("hello");
sendMsg.send(hwSocket);
sendMsg.destroy();
ZMsg msg = ZMsg.recvMsg(hwSocket, 0);
if(msg == null){
continue;
}
System.out.println(msg.toString());
msg.destroy();
}
hwSocket.close();
cxt.destroy();
}
}
public static void main(String[] args) {
ArrayList<Thread> thArray = new ArrayList<Thread>(5);
for(int i = 0 ;i< 5;i++){
Thread th = new Thread(new HwClientRunnable());
thArray.add(th);
th.start();
}
Thread bossThread = new Thread(new HwServerRunnable());
bossThread.start();
/*
try{
Thread.sleep(1000);
for(int i = 0;i<thArray.size();i++){
thArray.get(i).interrupt();
}
bossThread.interrupt();
}catch(Exception e){
e.printStackTrace();
}*/
}
}