实际上我想通过如下的 TCP 连接发送数据,但仍然有一些问题......我将服务器发送的字符串与客户端接收的字符串进行了比较,它们不匹配
服务器看起来像:
public class server {
static String aeskey;
static String secret;
public static void main(String[] args) throws Exception {
AES aes = new AES();
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(1234);
} catch (IOException e) {
System.err.println("Could not listen on port: 1234.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String int1,int2;
byte[] cipher = aes.encrypt("1234567890123456", "1234567890123456");
String s = new String(cipher , "ISO-8859-1");
out.println(s);
int1 = in.readLine();
System.out.println("From Client:"+int1);
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
客户端(在 Android 中尝试过)如下所示:
public class MainActivity extends Activity {
int cmd, ret;
public String IPAdress;
public String aeskey;
public String secret;
public Socket socket;
public PrintWriter out;
public BufferedReader in;
public AES aes = new AES();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button b1 = (Button)findViewById(R.id.button1);
cmd = 0;
b1.setText("Connect to server");
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(cmd == 0) ////setting-up the connection
{
int cs = connect_to_server();
if(cs == 0)
{
b1.setText("Connected");
try {
int dp = do_protocol();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cmd = 1;
}
}
else
{
///to be edited later
}
}
});
}
public int do_protocol() throws UnsupportedEncodingException
{
int d = 0;
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String num1 = null,num2 = null;
aeskey=aes.generateRandomString(16); ///generez o cheie AES
try {
num1 = in.readLine();
} catch (IOException e1) {
d = 1;
e1.printStackTrace();
}
byte[] cipher = null;
cipher = num1.getBytes("ISO-8859-1");
try {
num2 = new String(aes.decrypt(cipher, aeskey));
} catch (Exception e) {
d = 2;
e.printStackTrace();
}
out.println(num2);
return d;
}
public int connect_to_server()
{
ret = 0;
try {
socket = new Socket("192.168.1.144", 1234);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
Toast.makeText(this, "Unable to connect to server", Toast.LENGTH_LONG).show();
ret = 1;
} catch (IOException e) {
Toast.makeText(this, "Unable to connect to server", Toast.LENGTH_LONG).show();
ret = 2;
}
if(ret == 0)
Toast.makeText(this, "Connected to server", Toast.LENGTH_LONG).show();
return ret;
}