TCP连接上的java Caesar密码
我正在使用eclipse在安卓中构建这个应用程序
我正在构建一个程序,它接受一个编码的caesar密码字符串,找到移位数,然后将解码后的文本发回。我遇到了很多问题,所以我把程序分解成只解码两个字母的单词。我很少向客户机返回字符串,当我返回任何内容时,它通常为“null”。以下是我的课程:
此类解码消息:
package com.mafia.ceasarCipher;
import java.util.ArrayList;
import 安卓.util.Log;
public class ceasar{
static Boolean cDB = new Boolean( false );
int shift=0;
fileInput dict= new fileInput();
public String cDecode(String input){
char[] s = input.toCharArray();
int length=0;
for(int i=0;i < s.length;i++){
if(s[i]!= ' '){
length++;
}
}
String output;
//char[] five=new char[5];
//char[] four=new char[4];
//char[] three=new char[3];
char[] two=new char[2];
boolean word = false;
for(int i=0;i<s.length;i++){
if(length==2){
for(int j=0;j<2;j++){
two[j]=s[j];
if(isWord(input,2))
Log.d("find", "proper" + "" + "checkWord");
word=true;
}
}
if(word==true){
output=shiftWord(input,shift);
Log.d("find", "proper" + "" + "wordIsTrue");
cDB =true;
return output;
}}
output="These are not words";
cDB =true;
return output;
}
public boolean isWord(String s,int length){
ArrayList<String> d2=new ArrayList<String>();
d2=dict.readD2();
String st=s;
if(length==2){
for(int i=0; i<26;i++){
for(int j=0;j<d2.size();j++){
if(st==d2.get(j))
Log.d("find", "proper" + "" + "isWord");
return true;
}
st=shiftWord(st,2);
shift+=1;
}
}
return false;
}
public String shiftWord(String s,int length){
Log.d("find", "proper" + "" + "shiftWord");
char[] c=s.toCharArray();
for(int i=0;i<c.length;i++){
for(int j=0;j<length;j++){
if(c[i]=='a')
c[i]='z';
else if(c[i]=='b')
c[i]='a';
else if(c[i]=='c')
c[i]='b';
else if(c[i]=='d')
c[i]='c';
else if(c[i]=='e')
c[i]='d';
else if(c[i]=='f')
c[i]='e';
else if(c[i]=='g')
c[i]='f';
else if(c[i]=='h')
c[i]='g';
else if(c[i]=='i')
c[i]='h';
else if(c[i]=='j')
c[i]='i';
else if(c[i]=='k')
c[i]='j';
else if(c[i]=='l')
c[i]='k';
else if(c[i]=='m')
c[i]='l';
else if(c[i]=='n')
c[i]='m';
else if(c[i]=='o')
c[i]='n';
else if(c[i]=='p')
c[i]='o';
else if(c[i]=='q')
c[i]='p';
else if(c[i]=='r')
c[i]='q';
else if(c[i]=='s')
c[i]='r';
else if(c[i]=='t')
c[i]='s';
else if(c[i]=='u')
c[i]='t';
else if(c[i]=='v')
c[i]='u';
else if(c[i]=='w')
c[i]='v';
else if(c[i]=='x')
c[i]='w';
else if(c[i]=='y')
c[i]='x';
else if(c[i]=='z')
c[i]='y';
}
}
return c.toString();
}
}
这是服务器
package com.mafia.ceasarCipher;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import 安卓.util.Log;
public class Server implements Runnable {
// Public boolean cDecodeStatus= new boolean();
public static final String SERVERIP = "127.0.0.1"; // 'Within' the emulator!
public static final int SERVERPORT = 4444;
public ceasar c = new ceasar();
public int byteCounter;
public void run ()
{
try{
String clientSentence;
String modifiedSentence;
ServerSocket welcomeSocket = new ServerSocket(SERVERPORT);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
Log.e( "Jake" ,"RECIEVED from client" + clientSentence);
//modifiedSentence = clientSentence.toUpperCase() + '\n';
modifiedSentence = c.cDecode(clientSentence );
Log.d("face", modifiedSentence);
System.out.println("before while"+' '+ ceasar.cDB);
while(ceasar.cDB!=true){
//System.out.println("before if"+' '+ ceasar.cDB);
//if(ceasar.cDB==true)
//Log.d("send", "sending Loop");
//System.out.println(ceasar.cDB);
outToClient.writeBytes(modifiedSentence);
}
}
}
catch (Exception e) {
Log.e("UDP", "S: Error", e);
}
}
}
这是一门阅读中字典的课程
package com.mafia.ceasarCipher;
import java.io.*;
import 安卓.os.Environment;
import 安卓.util.Log;
import java.util.ArrayList;
public class fileInput
{
public ArrayList<String> readD2(){
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"d2.txt");
//Read text from file
try {
BufferedReader br = new BufferedReader(new FileReader(file));
ArrayList<String> x = new ArrayList <String>() ;
String str;
str = br.readLine();
while(str != null) {
str = br.readLine();
x.add(str) ;
}
return x;
}
catch (IOException e) {
return null;
}
}
这是客户端
package com.mafia.ceasarCipher;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import 安卓.util.Log;
public class Client implements Runnable {
static String messageInput = new String();
// static String modifiedSentence= new String();
/*public static String getOutput(){
return modifiedSentence;
}*/
public static void setCode(String x)
{
messageInput= x;
}
@Override
public void run() {
try{
Log.d("Step", "1");
Socket clientSocket = new Socket("localhost", 4444);
Log.d("Step", "2");
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
Log.d("Step", "3");
Log.d("Step", "4");
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
Log.d("Step", "5");
outToServer.writeBytes(messageInput + '\n');
Log.d("Step", "6");
String modifiedSentence;
modifiedSentence = inFromServer.readLine();
Log.d("Step", "7");
Log.e( "Jake" ,"RECIEVED BACK FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
catch (Exception e) {
Log.e("UDP", "C: Error", e);}
}
}
此类处理onCreate和onClick方法
package com.mafia.ceasarCipher;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
public class udpConnection extends Activity {
String input = "";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Kickoff the Server, it will
* be 'listening' for one client packet */
new Thread(new Server()).start();
final EditText et = (EditText)findViewById(R.id.editText1);
input = et.getText().toString();
/*Log.d("UDP","input is"+ input);
final TextView TV = (TextView)findViewById(R.id.textView2);
String out= Client.getOutput();
TV.setText(out);*/
/* GIve the Server some time for startup */
try {
Thread.sleep(500);
} catch (InterruptedException e) { }
// Kickoff the Client
new Thread(new Client()).start();
Client.setCode(input );
}
});
}
public String getInput()
{
return input;
}
}
图形用户界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical" 安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent">
<RelativeLayout 安卓:id="@+id/mainLayout"
安卓:layout_height="fill_parent" 安卓:layout_width="fill_parent">
<TextView 安卓:layout_height="wrap_content"
安卓:layout_alignParentTop="true" 安卓:id="@+id/textView1"
安卓:layout_width="fill_parent" 安卓:gravity="center"
安卓:textSize="24px" 安卓:text="Networking" />
<EditText 安卓:layout_height="wrap_content" 安卓:id="@+id/editText1"
安卓:layout_width="wrap_content" 安卓:layout_below="@+id/textView1"
安卓:layout_alignLeft="@+id/textView1" 安卓:layout_alignRight="@+id/textView1"
安卓:hint="Enter coded text here!" 安卓:isScrollContainer="true" 安卓:editable="true"/>
<Button 安卓:layout_height="wrap_content"
安卓:layout_width="wrap_content" 安卓:id="@+id/button1"
安卓:layout_below="@+id/editText1" 安卓:layout_alignLeft="@+id/editText1"
安卓:layout_alignRight="@+id/editText1" 安卓:text="Send message" />
<TextView 安卓:layout_height="wrap_content"
安卓:layout_width="wrap_content" 安卓:layout_below="@+id/button1"
安卓:id="@+id/textView2" 安卓:layout_alignLeft="@+id/button1"
安卓:layout_alignRight="@+id/button1" 安卓:hint="Message will show here!"
安卓:editable="true" 安卓:isScrollContainer="true"/>
<Button 安卓:layout_below="@+id/textView2" 安卓:id="@+id/button2" 安卓:layout_height="wrap_content" 安卓:layout_width="wrap_content" 安卓:layout_alignLeft="@+id/textView2" 安卓:layout_alignRight="@+id/textView2" 安卓:text="Display Text"></Button>
</RelativeLayout>
</LinearLayout>
# 1 楼答案
首先,我不认为它返回空值。我认为你不能把一个字符数组转换成那样的字符串。这是将字符数组转换为字符串的正确方法
用shiftWord方法试试这个。请注意,我所做的唯一更改是: 添加一个名为d的字符串,然后在底部将新字符串d设置为您的字符数组,然后返回字符串d
其次,我不明白为什么在shiftWord方法中有嵌套循环。我认为,如果你删除第二个,让第一个保持不变,那么它应该会工作
免责声明:我没有测试过这一切:)祝你好运