客户端/服务器程序,客户端将文本文件发送到服务器。服务器获取文件,读取文件并计算数字,字符,空格等数量,然后发送回客户端。
程序在将文件发送到服务器时运行良好,或者在计数后使服务器向客户端发送字符串,但不能同时运行。
客户端
WHERE 1 = 1
服务器
import java.io.*;
import java.net.*;
import java.util.Scanner;
class TCPClient
{
public static void main(String argv[]) throws Exception
{
System.out.println("I am client");
Socket clientSocket = new Socket("localhost", 9881);
File file = new File("D:\\test.txt");
byte[] bytearray = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedOutputStream outToServer = new BufferedOutputStream(clientSocket.getOutputStream());
bis.read(bytearray, 0, bytearray.length);
outToServer.write(bytearray, 0, bytearray.length);
outToServer.flush();
System.out.println("From server: " + '\n');
String x;
while((x = inFromServer.readLine()) != null)
{
System.out.println(x);
}
clientSocket.close();
}
}
客户端的输出将是
class TCPServer
{
//***** Functions for counting *****
public static int number(String x)
{
int numCount = 0;
char temp;
for( int i=0; i <x.length(); i++)
{
temp = x.charAt(i);
if(Character.isDigit(temp))
{
numCount++;
}
}
return numCount;
}
public static int character(String x)
{
int charCount = 0;
char temp;
for( int i=0; i <x.length(); i++)
{
temp = x.charAt(i);
if(Character.isLetter(temp))
{
charCount++;
}
}
return charCount;
}
public static int space(String x)
{
int spaceCount = 0;
char temp;
for (int i=0; i <x.length(); i++)
{
temp = x.charAt(i);
if(temp==' ')
{
spaceCount++;
}
}
return spaceCount;
}
public static int special(String x)
{
int specialCount = 0;
char temp;
for (int i=0; i <x.length(); i++)
{
temp = x.charAt(i);
if(temp=='!'||temp=='@'||temp=='#'||temp=='$'||temp=='%'||temp=='&'||temp==':'||temp=='^'||temp=='*')
{
specialCount++;
}
}
return specialCount;
}
//***************************************
public static void main(String argv[]) throws Exception
{
System.out.println("I am Server");
ServerSocket welcomeSocket = new ServerSocket(9881);
System.out.println("After Welcome");
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
System.out.println("After Accept");
PrintWriter outToClient = new PrintWriter(connectionSocket.getOutputStream());
byte[] aByte = new byte [1];
int bytesRead;
InputStream is = connectionSocket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileOutputStream fos = new FileOutputStream ("D:\\server.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(aByte, 0, aByte.length);
while (bytesRead != -1)
{
baos.write(aByte);
bytesRead = is.read(aByte);
}
bos.write(baos.toByteArray());
bos.flush();
BufferedReader br = null;
try
{
String sCurrentLine;
br = new BufferedReader(new FileReader("D:\\server.txt"));
while ((sCurrentLine = br.readLine()) != null)
{
System.out.println(sCurrentLine);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
/*******Functions********/
int numCount = 0;
Scanner sc = new Scanner(new FileReader("D:\\server.txt"));
while (sc.hasNextLine())
{
String line;
line = sc.nextLine();
numCount += number(line);
}
int charCount = 0;
Scanner sc2 = new Scanner(new FileReader("D:\\server.txt"));
while (sc2.hasNextLine())
{
String line;
line = sc2.nextLine();
charCount += character(line);
}
int spaceCount = 0;
Scanner sc3 = new Scanner(new FileReader("D:\\server.txt"));
while(sc3.hasNextLine())
{
String line;
line = sc3.nextLine();
spaceCount += space(line);
}
int specialCount = 0;
Scanner sc4 = new Scanner(new FileReader("D:\\server.txt"));
while(sc4.hasNextLine())
{
String line;
line = sc4.nextLine();
specialCount += special(line);
}
int lineCount = 0;
Scanner sc5 = new Scanner(new FileReader("D:\\server.txt"));
while(sc5.hasNextLine())
{
String line;
line = sc5.nextLine();
if(!"".equals(line.trim()))
{
lineCount++;
}
}
//System.out.println(numCount);
//System.out.println(charCount);
//System.out.println(spaceCount);
//System.out.println(specialCount);
//System.out.println(lineCount);
String x="Numbers: " + numCount + "\nCharacters: " + charCount + "\nSpaces: " + spaceCount + "\nSpecials: " + specialCount + "\nLines: " + lineCount + '\n';
//System.out.println(x);
outToClient.println(x + '\n');
outToClient.flush();
outToClient.close();
bos.close();
/***********************/
connectionSocket.close();
}
}
}
如果我的代码很乱,我很抱歉,这是我第一次使用Java。所以我希望你们可以帮助我牢记我对Java的了解不多。
感谢您的时间。