자바 쓰레드 관련 서버&클라이언트 통신
냐하
*handler 소스
import java.io.*;
import java.net.*;
import java.util.*;
public class handler extends thread
{
protected static bufferedreader netin = null;
protected static printwriter netout = null;
protected static bufferedreader conin = null;
protected static printwriter conout = null;
protected static final string eor = end_of_response;
protected static file currentdir = null;
protected socket client;
public handler(string name)
{
super(name);
}
public synchronized void setsocket(socket client)
{
this.client = client;
notify();
}
public boolean isworking()
{
return (client != null);
}
public void run()
{
while(true)
{
synchronized(this)
{
try{
wait();
}catch(interruptedexception ie){}
}
try
{
netin = new bufferedreader(new inputstreamreader(client.getinputstream()));
netout = new printwriter(new outputstreamwriter(client.getoutputstream()));
conin = new bufferedreader(new inputstreamreader(system.in));
conout = new printwriter(new outputstreamwriter(system.out));
}
catch (ioexception ioe)
{
system.err.println(failed to get stream...);
system.exit(1);
}
netout.println(welcome to the filemanager server...);
netout.println(eor);
netout.flush();
currentdir = new file(system.getproperty(user.dir));
string cmdline;
while(true) {
try {
cmdline = netin.readline();
processcmd(cmdline);
} catch(ioexception ioe) {
system.err.println(exception in processing command...);
system.exit(1);
}
}
}
}
static void processcmd(string cmdline) {
if(cmdline == null) {
cmdbadcommand();
netout.println(eor);
netout.flush();
return;
}
stringtokenizer tokens = new stringtokenizer(cmdline.trim());
string cmd = null;
string arg1 = null;
string arg2 = null;
if(tokens.hasmoretokens()) {
cmd = tokens.nexttoken();
if(tokens.hasmoretokens()) {
arg1 = tokens.nexttoken();
}
if(tokens.hasmoretokens()) {
arg2 = tokens.nexttoken();
}
} else {
cmdbadcommand();
netout.println(eor);
netout.flush();
return;
}
if(cmd.equals(help)) {
cmdhelp();
} else if((cmd.equals(history)) ||cmd.equals(h)) {
cmdhistory();
} else if((cmd.equals(list)) ||cmd.equals(l)) {
cmdlist();
} else if((cmd.equals(rename)) ||cmd.equals(ren)) {
cmdrename(arg1, arg2);
} else if((cmd.equals(add)) ||cmd.equals(a)) {
cmdadd(arg1, arg2);
} else if((cmd.equals(sub)) ||cmd.equals(s)) {
cmdsub(arg1, arg2);
} else if((cmd.equals(mul)) ||cmd.equals(m)) {
cmdmul(arg1, arg2);
} else if((cmd.equals(div)) ||cmd.equals(d)) {
cmddiv(arg1, arg2);
} else if((cmd.equals(delete)) ||cmd.equals(del)) {
cmddelete(arg1);
} else if((cmd.equals(makedir)) ||cmd.equals(md)) {
cmdmakedir(arg1);
} else if((cmd.equals(changedir)) ||cmd.equals(cd)) {
cmdchangedir(arg1);
} else if((cmd.equals(find)) ||cmd.equals(f)) {
cmdfind(arg1);
} else if((cmd.equals(type)) ||cmd.equals(t)) {
cmdtype(arg1);
} else {
cmdbadcommand();
}
netout.println(eor);
netout.flush();
}
static void cmdhelp() {
netout.println(file manager command);
netout.println(---------------------------------- );
netout.println( help );
netout.println( * history(h) );
netout.println( list(l) );
netout.println( rename(ren) [old name] [new name] );
netout.println( * add(a) [data1] [data2] );
netout.println( * sub(s) [data1] [data2] );
netout.println( * mul(m) [data1] [data2] );
netout.println( * div(d) [data1] [data2] );
netout.println( find(f) [filename/dir name] );
netout.println( * type(t) [filename] );
netout.println( delete(del) [filename/dir name] );
netout.println( makedir(md) [dir name] );
netout.println( changedir(cd) [dir name] );
netout.println(---------------------------------- );
netout.flush();
}
static void cmdhistory() {
netout.println(---------------------------------- );
netout.println(my name is kong jin san);
netout.println(---------------------------------- );
netout.flush();
}
static void cmddelete(string arg) {
if(arg == null) {
cmdbadcommand();
return;
}
file file = new file(currentdir, arg);
if(file.delete()) {
netout.println(file + file.getname() + deleted... );
netout.flush();
} else {
netout.println(failed to delete file... );
netout.flush();
}
}
static void cmdrename(string arg1, string arg2) {
if((arg1 == null) || (arg2 == null)) {
cmdbadcommand();
return;
}
file file = new file(currentdir, arg1);
file target = new file(currentdir, arg2);
if(target.exists()) {
target.delete();
}
file.renameto(target);
netout.println(file + arg1 + - + arg2 );
netout.flush();
}
static void cmdadd(string arg1, string arg2) {
if((arg1 == null) || (arg2 == null)) {
cmdbadcommand();
return;
}
int d1, d2, answer;
d1 = integer.parseint(arg1);
d2 = integer.parseint(arg2);
answer = d1 + d2;
netout.println( d1 + + + d2 + = + answer);
netout.flush();
}
static void cmdsub(string arg1, string arg2) {
if((arg1 == null) || (arg2 == null)) {
cmdbadcommand();
return;
}
int d1, d2, answer;
d1 = integer.parseint(arg1);
d2 = integer.parseint(arg2);
answer = d1 - d2;
netout.println( d1 + - + d2 + = + answer);
netout.flush();
}
static void cmdmul(string arg1, string arg2) {
if((arg1 == null) || (arg2 == null)) {
cmdbadcommand();
return;
}
int d1, d2, answer;
d1 = integer.parseint(arg1);
d2 = integer.parseint(arg2);
answer = d1 * d2;
netout.println( d1 + x + d2 + = + answer);
netout.flush();
}
static void cmddiv(string arg1, string arg2) {
if((arg1 == null) || (arg2 == null)) {
cmdbadcommand();
return;
}
int d1, d2, answer;
d1 = integer.parseint(arg1);
d2 = integer.parseint(arg2);
answer = d1 / d2;
netout.println( d1 + / + d2 + = + answer);
netout.flush();
}
static void cmdlist() {
file[] list = currentdir.listfiles();
netout.println(file list in + currentdir.getname() );
netout.println(====ln(================================================== );
for(int i=0 ; ilist.length ; i++) {
if(list[i].isfile()) {
netout.println(list[i].getname() + \t\t + (list[i].canread()?r:_) + (list[i].canwrite()?w:_) + + list[i].length() + \t + (list[i].ishidden()?hidden:) );
netout.flush();
} else if(list[i].isdirectory()) {
netout.println([ + list[i].getname() + ]\t\t + (list[i].canread()?r:_) + (list[i].canwrite()?w:_) );
netout.flush();
}
}
netout.println(================================================== );
netout.println(total + list.length + items listed... );
netout.flush();
}
static void cmdtype(string arg) {
if(arg == null) {
cmdbadcommand();
return;
}
try{
filereader filein = new filereader(arg);
int c;
while ((c = filein.read()) != -1) {
netout.write((char) c);
}
netout.write(eor);
netout.flush();
netout.close();
} catch(ioexception ignored) {}
}
static void cmdmakedir(string arg) {
if(arg == null) {
cmdbadcommand();
return;
}
file dir = new file(currentdir, arg);
if(dir.exists()) {
// does nothing
} else {
dir.mkdir();
}
netout.println(directory + arg + constructed... );
netout.flush();
}
static void cmdchangedir(string arg) {
if(arg == null) {
cmdbadcommand();
return;
}
file newdir = new file(currentdir, arg);
if(!(newdir.exists()) || !(newdir.isdirectory())) {
netout.println(invalid directory... );
netout.flush();
} else {
currentdir = newdir;
netout.println(moved to + arg );
netout.flush();
}
}
static void cmdfind(string arg) {
if(arg == null) {
cmdbadcommand();
return;
}
file file = new file(arg);
if(file.exists()) {
netout.println(file + arg + exists...);
netout.flush();
} else {
netout.println(file + arg + not found);
netout.flush();
}
}
static void cmdbadcommand() {
netout.println(bad command...);
netout.flush();
}
static void getresponse() throws ioexception {
string line;
while(!((line = netin.readline()).equals(eor))) {
conout.println(line);
}
conout.flush();
}
}
*testserver3 소스
import java.io.*;
import java.net.*;
import java.util.*;
public class testserver3 {
public static void main(string[] args) {
system.out.println(file manager server program...);
serversocket server = null;
handler[] handlers = new handler[10];
for (int i = 0;i10 ;i++ )
{
handlers[i] = new handler(handler + i);
handlers[i].start();
}
try {
server = new serversocket(5264);
while(true)
{
socket client = server.accept();
int r;
do{
r=(int)(math.random()*10);
}while(handlers[r].isworking());
handlers[r].setsocket(client);
}
} catch(ioexception ioe) {
system.err.println(exception generated...);
}
finally
{
try{
server.close();
}catch(ioexception ignored){}
}
}
}
*testclient3 소스
import java.io.*;
import java.net.*;
import java.util.*;
public class testclient3
{
protected static bufferedreader netin = null;
protected static printwriter netout = null;
protected static bufferedreader conin = null;
protected static printwriter conout = null;
protected static final string eor = end_of_response;
public static void main(string[] args)
{
if(args.length 0) {
system.err.println(syntax: java testclient3+ [server address]);
system.exit(1);
}
if(args.length 0) {
fmclient(args[0]);
} else {
system.err.println(invalid command-line arguments);
system.exit(1);
}
}
static void fmclient(string addr) {
system.out.println(file manager client program...);
socket socket = null;
try {
socket = new socket(addr, 5264);
system.out.println(connected to... +
socket.getinetaddress().gethostname());
} catch(ioexception ioe) {
system.err.println(failed to connect to server...);
system.exit(1);
}
try {
netin = new bufferedreader(new inputstreamreader(socket.getinputstream()));
netout = new printwriter(new outputstreamwriter(socket.getoutputstream()));
conin = new bufferedreader(new inputstreamreader(system.in));
conout = new printwriter(new outputstreamwriter(system.out));
} catch(ioexception ioe) {
system.err.println(failed to get stream...);
system.exit(1);
}
try {
string line;
while( !(line = netin.readline()).equals(eor) ) {
conout.println(line);
conout.flush();
}
while(true) {
conout.print(command );
conout.flush();
line = conin.readline();
netout.println(line );
netout.flush();
getresponse();
conout.println();
conout.flush();
}
} catch(ioexception ioe) {
system.err.println(exception in communication...);
system.exit(1);
}
}
static void getresponse() throws ioexception {
string line;
while(!((line = netin.readline()).equals(eor))) {
conout.println(line);
}
conout.flush();
}
}testserver3을 실행시켜놓고
2~3개정도 testclient3를 localhost로 접속을 합니다.
접속을 하게된후에 명령어를 치면
예를 들어 첫번째클라이언트로 help를 치고,
다른 클라이언트에서는 histroy를 치게되면 한쪽 클라이언트는 먹통이되고
첫번째클라이언트에서 histroy정보가 뜹니다.
다중 쓰레드를 사용하여 코딩을 했는데...클라이언트가 두개이상되면 왜 명령어가 안먹히코 무한룹을 도는지
궁금합니다.....다른 오류는 없습니다...
testserver3을 실행시켜놓고
2~3개정도 testclient3를 localhost로 접속을 합니다.
접속을 하게된후에 명령어를 치면
예를 들어 첫번째클라이언트로 help를 치고,
다른 클라이언트에서는 histroy를 치게되면 한쪽 클라이언트는 먹통이되고
첫번째클라이언트에서 histroy정보가 뜹니다.
다중 쓰레드를 사용하여 코딩을 했는데...클라이언트가 두개이상되면 왜 명령어가 안먹히코 무한룹을 도는지
궁금합니다.....다른 오류는 없습니다...