MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

Revision 3 as of 2014-09-26 23:59:47
  • Java

Java

Regular expressions

   1 /*
   2 javac TesteRe.java 
   3 java -cp . TesteRe 
   4 */
   5 /*
   6 gcj -C TesteRe.java  # compile with gcj, GNU compiler for Java
   7 gij -cp . TesteRe # run with gij, GNU interpreter for Java
   8 */
   9 
  10 import java.util.regex.Pattern;
  11 import java.util.regex.Matcher;
  12 
  13 public class TesteRe{
  14 
  15     public static void main(String args[]){ 
  16         String values[]={"aa12.txt","a123sss.txt","bs11bb.txt","123aaaa.sql","a12.txt","aaaa12","20ghj","6657"};
  17         Pattern a = Pattern.compile("^(\\D+)(\\d+)(\\D+)$");
  18         Pattern b = Pattern.compile("^(\\d+)(\\D+)$");
  19         Pattern c = Pattern.compile("^(\\D+)(\\d+)$");
  20         Pattern d = Pattern.compile("^(\\d+)$");
  21 
  22         for(String item:values){
  23             Matcher ma = a.matcher(item);
  24             Matcher mb = b.matcher(item);
  25             Matcher mc = c.matcher(item);
  26             Matcher md = d.matcher(item);
  27 
  28            if(ma.matches()){ 
  29                int val = Integer.parseInt(ma.group(2)) + 1;
  30                System.out.println(String.format("A: mv %s %s%d%s",item,ma.group(1),val,ma.group(3)  ) );
  31            }
  32 
  33            if(mb.matches()){ 
  34                int val = Integer.parseInt(mb.group(1)) + 1;
  35                System.out.println(String.format("B: mv %s %d%s",item, val , mb.group(2)  ) );
  36            }
  37 
  38            if(mc.matches()){ 
  39                int val = Integer.parseInt(mc.group(2)) + 1;
  40                System.out.println(String.format("C: mv %s %s%d",item,mc.group(1),val   ) );
  41            }
  42 
  43            if(md.matches()){ 
  44                int val = Integer.parseInt(md.group(1)) + 1;
  45                System.out.println(String.format("D: mv %s %d",item, val  ) );
  46            }
  47 
  48         }
  49     }
  50 }

Server/client

Server

   1 import java.net.ServerSocket;
   2 import java.net.Socket;
   3 import java.lang.Thread;
   4 import java.util.Stack;
   5 
   6 public class SocketServer{
   7     public static void main(String []args){
   8         ServerSocket ss = null; 
   9         int nrThreads= Runtime.getRuntime().availableProcessors() * 2; 
  10         System.out.println("Nr cores:" + nrThreads);
  11         ConnHandler[] c = new ConnHandler[nrThreads];
  12         for(int i=0;i<nrThreads;i++){
  13             c[i]=new ConnHandler();
  14             c[i].start();
  15         }
  16         
  17         try{ 
  18             ss = new ServerSocket(1234); 
  19         }
  20         catch(Exception ex){    }
  21         long count=0;
  22         while(true){
  23           try{
  24             Socket s = ss.accept(); 
  25             int idx = (int)count%nrThreads;
  26             c[ idx  ].addSocket(s);
  27             count++;
  28             
  29           }
  30           catch(Exception ex){
  31               System.out.println(ex.getMessage());
  32           }
  33         }
  34     }
  35 }

ConnHandler

   1 import java.net.ServerSocket;
   2 import java.net.Socket;
   3 import java.lang.Thread;
   4 import java.util.Stack;
   5 
   6 public class ConnHandler extends Thread{
   7     private byte[] rx;
   8     private Stack stack;
   9     long tid;
  10 
  11     public ConnHandler(){
  12         this.rx= new byte[1024];
  13         this.stack = new Stack();
  14     }
  15 
  16     public void addSocket(Socket s){
  17         this.stack.push(s);
  18     }
  19 
  20     public void run(){
  21         this.tid = Thread.currentThread().getId();
  22 
  23         while(true){
  24             if( this.stack.empty() == false){
  25                 Socket s = (Socket)this.stack.pop();
  26                 try{
  27                     if( s.getInputStream().available() > 0 ){
  28                         int readedBytes = s.getInputStream().read( rx );
  29                          System.out.println("ThreadId:"+tid  + " ReadedBytes:"+ readedBytes + 
  30 " StackSize:" + this.stack.size() );
  31                     }
  32                     s.close();
  33                 }
  34                 catch(Exception ex){
  35                 } 
  36             }
  37             else{ try{ Thread.sleep(500); }catch(Exception ex){} }
  38         }
  39     }
  40 }

Client (python)

   1 import threading
   2 import time
   3 import socket
   4 
   5 class Client (threading.Thread):
   6     def __init__(self):
   7         threading.Thread.__init__(self) #required
   8 
   9     def run(self):
  10         for x in range(1,100):
  11             HOST = 'localhost'
  12             PORT = 1234
  13             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  14             print('connecting...')
  15             s.connect((HOST, PORT))
  16             print('sending config...')
  17             s.send('test\r\n')
  18             s.close()
  19             print('complete')
  20 
  21 if __name__=='__main__':
  22     clients=[]
  23     for x in range(1,100):
  24         clients.append( Client() )
  25     for c in clients:
  26         c.start()
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01