我正在尝试通过网络传输一系列字符串。我可以发送一个字符串,但是一旦我尝试发送一个字符串数组,我就会遇到问题。我做了一些研究并提出了修复程序,该修复程序在 IDE 中没有错误,但是当我在外部运行它时程序崩溃了。我已将其缩小为我意外创建的无限循环。我将粘贴所有代码,因为这里放太多了。
这是我要实现的目标的摘要...
- 打开文本文件
- 逐行读取(将每一行写入数组内的单独字符串)
- 文件读取完成后,开始流式传输每个单独的字符串
- 在接收端有一个 for 循环,将字符串放回另一个数组
- 最后,在服务器端,将字符串的每个部分分成 5 个不同的字符串
这是我的客户类:
public class Transfers {
public int port = 1223;
//public String Ip = LoginForm.IP;
//public String ip = null;
public static Socket login = null;
public static Socket sock = null;
public static PrintWriter outStream = null;
private static BufferedReader inStream = null;
private static boolean ON = false;
public static String authorize = null;
public static boolean connected = true;
public static void transfers(String IP, int port, String content) throws UnknownHostException, IOException {
try {
login = new Socket(IP, port);
//System.out.println("Connected for streaming");
outStream = new PrintWriter(login.getOutputStream(), true);
outStream.println(content);
} catch (UnknownHostException ex) {
Logger.getLogger(Transfers.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Transfers.class.getName()).log(Level.SEVERE, null, ex);
login.close();
}
}
public static String[] recieveArray(String IP) throws UnknownHostException, IOException {
String[] farray = null;
sock = new Socket(IP, 1224);
inStream = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String count = inStream.readLine();
int counter = Integer.parseInt(count);
System.out.println("counter");
for (int i = 0; i < counter; i++) {
inStream = new BufferedReader(new InputStreamReader(sock.getInputStream()));
farray[i] = inStream.readLine();
System.out.println(farray[i]);
}
return farray;
}
}
这是我的服务器类:
public class Specials {
private static ServerSocket server = null;
private static Socket client = null;
private static PrintWriter outStream = null;
private static BufferedReader inStream = null;
private static boolean ServerOn = true;
public static String message = "";
public static String command = null;
static public InetAddress IP = null;
public static String status = null;
private static String file = "accounts.dat";
private static int counter;
public static void arraysend(String filename) throws FileNotFoundException, IOException {
counter = 0;
FileInputStream fstream = new FileInputStream("AppData/" + filename);
String strLine;
String[] filearray;
try (DataInputStream in = new DataInputStream(fstream)) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
filearray = new String[1000];
for (int j = 0; j < 10; j++) {
filearray[j] = br.readLine();
counter++;
}
in.close();
}
try {
server = new ServerSocket(1224);
client = server.accept();
IP = client.getInetAddress();
outStream = new PrintWriter(client.getOutputStream(), true);
filearray[0] = Integer.toString(counter);
outStream.println(filearray[0]);
for (int i = 1; i < counter; i++) {
outStream = new PrintWriter(client.getOutputStream(), true);
outStream.println(filearray[i]);
}
client.close();
server.close();
} catch (IOException ex1) {
Logger.getLogger(Specials.class.getName()).log(Level.SEVERE, null, ex1);
}
}
}
我没有收到任何错误消息,应用程序只是崩溃了。为了方便起见,我在下面包含了我的代码的 pastebins。
任何帮助将不胜感激,谢谢。
C