假設我在Server端有以下的Code
HashMap<String,String> map=new HashMap<String,String> ();
ObjectOutputStream oos=new ObjectOutputStream(clientsocket.getOutputStream);
while(true){
map.put(Long.toString(System.currentTimeMillis()),Long.toString(System.currentTimeMillis()));
oos.writeObject(map);
}
另外一端收到的會發現後面收到的物件都跟第一次收到的物件一樣,Why?因為在送出的時候都是指向同一個位址,因此必須要加上oos.reset();結果才會正確。
2011年2月25日 星期五
2011年2月7日 星期一
File Transfer via Network using C vs. JAVA
這次的測試分別比較了使用gcc /gcc -O2 編譯而成的與javac 1.6.0_23 的版本編譯而成的執行檔來做比較,測試的兩台機器是在Lan環境下。測試的設備與程式碼如下:
測試程式如下:
C (copy from http://content.edu.tw/primary/info_edu/cy_sa/report/more/8611a8.htm)
server.c
server.java
client.java
很詭異的結果,再研究看看。
CPU | Intel(R) Core(TM) i7 CPU 950 @ 3.07GHz |
MEMERY | 6G |
NETWORK | 1G |
TEST FILE | 466819755 byte (tar.bz2 file) |
測試程式如下:
C (copy from http://content.edu.tw/primary/info_edu/cy_sa/report/more/8611a8.htm)
server.c
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h> #define TRUE 1 main() { int sock, msgsock, length; struct sockaddr_in name; FILE *fp; int rval,wrval; char buf[1024],filename[1024]; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("opening stream socket"); exit(1); } /*-------------------------------------------------------*/ /* Create the acceptable connection host with Wildcard */ /*-------------------------------------------------------*/ name.sin_family = AF_INET; name.sin_addr.s_addr = INADDR_ANY; name.sin_port = 5000; if (bind(sock, (struct sockaddr *)&name, sizeof name) < 0) { perror ("binding stream socket"); exit(1); } listen(sock,5); do{ msgsock = accept(sock,(struct sockaddr *)0, (int *)0); if (msgsock == -1) perror("accept error !\\n "); else do { bzero(buf, sizeof(buf) ); /*--------------------------------------------------*/ /* Read filename from msgsock (sent from client) */ /*--------------------------------------------------*/ if ((rval = read(msgsock, buf,1024)) < 0) perror("reading stream message"); if (rval == 0) printf("Ending connection\\n"); else { if ((fp=fopen(buf,"r")) == NULL) { printf("file not found : %s !\\n ",buf); break; } printf("opening [%s] !!\\n ",buf); bzero(filename,sizeof(filename)); strcpy(filename, buf); /*--------------------------------------------------*/ /* Read file content & sent to msgsock ...> client */ /*--------------------------------------------------*/ while (fgets(buf,sizeof(buf),fp) != (char*)NULL) { if ((wrval=write(msgsock,buf,sizeof(buf))) <0) break; //printf("."); } fclose(fp); printf("\\n\\n closing [%s] ! \\n",filename); rval=0; } }while (rval != 0); close(msgsock); printf(" [%s]finish* !! \\n ",filename); }while (TRUE); close(sock); exit(0); }client.c
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #define DATA "The sea is clam ...." main(int argc, char *argv[] ) { int sock, rval, wrval; char buf[1024]; struct sockaddr_in server; struct hostent *hp, *gethostbyname(); FILE *fp; sock = socket(AF_INET,SOCK_STREAM,0); if (sock < 0) { perror("opening stream socket"); exit(1); } server.sin_family=AF_INET; hp=gethostbyname(argv[1]); if (hp == 0 ) { printf( "%s : unknown host\\n", argv[1]); exit(2); } bcopy((char*)hp->h_addr, (char *)&server.sin_addr, hp->h_length); server.sin_port = 5000; if (connect(sock, (struct sockaddr *)&server, sizeof server) < 0) { perror ("connecting stream socket"); exit(1); } if (write(sock, argv[3],strlen(argv[3])) < 0) perror ("writing stream socket"); sleep(1); printf("requiring [%s], ...\\n",argv[3]); fp=fopen(argv[4],"w+"); /*---------------------------------------------------------*/ /* Read file content from sock (sent from ftp_serv ) */ /* and Write to local file */ /*---------------------------------------------------------*/ while ( (rval=read(sock,buf,sizeof(buf) )) >1) { //printf("."); fputs(buf,fp); } fclose(fp); close(sock); printf("finish !! \\n"); printf("closing connection !! \\n"); exit(0); }JAVA
server.java
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class server { public static void main(String[] args) throws IOException { ServerSocket servsock = new ServerSocket(5000); File myFile = new File("hadoop.tar.bz2"); while (true) { Socket sock = servsock.accept(); byte[] mybytearray = new byte[(int) myFile.length()]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile)); bis.read(mybytearray, 0, mybytearray.length); OutputStream os = sock.getOutputStream(); os.write(mybytearray, 0, mybytearray.length); os.flush(); sock.close(); } } }
client.java
import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.net.Socket; public class client { public static void main(String[] argv) throws Exception { Socket sock = new Socket("192.168.254.1", 5000); byte[] mybytearray = new byte[1024]; InputStream is = sock.getInputStream(); FileOutputStream fos = new FileOutputStream("hadoop.tar.bz2"); BufferedOutputStream bos = new BufferedOutputStream(fos); int bytesRead ; while((bytesRead= is.read(mybytearray, 0, mybytearray.length))!=-1){ bos.write(mybytearray, 0, bytesRead); } bos.close(); sock.close(); } }測試結果如下:
Test Case | Time(s) |
C with -O2 | 20.539(s) |
C without -O2 | 20.286(s) |
JAVA | 4.923(s) |
訂閱:
文章 (Atom)