-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_client.java
70 lines (58 loc) · 1.81 KB
/
Main_client.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.mycalc.app;
import java.io.*;
import java.net.*;
import java.util.Arrays;
import java.util.Scanner;
public class Main{
static String read_one_msg(DataInputStream dis) throws IOException{
int b;
String str = "";
while(true){
if((b = dis.read())=='\0'){
break;
}
str+=(char)b;
}
return str;
}
static Socket s_main;
static DataInputStream dis;
static DataOutputStream dos;
public static void main( String[] args ){
try{
s_main = new Socket(args[0],Integer.valueOf(args[1]));
dis = new DataInputStream(s_main.getInputStream());
dos = new DataOutputStream(s_main.getOutputStream());
}
catch(IOException ie){
System.out.println("Cannot connect to server with these values of ip and port");
return;
}
Scanner scanner = new Scanner(System.in);
String str_send,str_recv;
while(true){
System.out.print("> ");
str_send = scanner.next();
try{
dos.write((str_send+"\0").getBytes());
//dis.read();
}
catch(IOException io){
System.out.println("Server Quit: 1");
return;
}
if(str_send.equals("exit") || str_send.equals("admin:exit")){
try{s_main.close();}catch(IOException e){}
return;
}
try{
str_recv = read_one_msg(dis);
}
catch(IOException io){
System.out.println("Server Quit: 2");
return;
}
System.out.println("" + str_recv);
}
}
}