在java中通过UDP传输文件
我用Java实现了以下使用TCP/IP的算法:在java中通过UDP传输文件,java,sockets,udp,tcp,Java,Sockets,Udp,Tcp,我用Java实现了以下使用TCP/IP的算法: -Client request a file -Server checks if the file exists - if do: send contents of the file to the client - if not: send "file not found" msg to the client 现在我很难用UDP数据包实现这个功能。这是我的密码: 客户: 服务器: 我建议查看UDP协议(),然后使用
-Client request a file
-Server checks if the file exists
- if do: send contents of the file to the client
- if not: send "file not found" msg to the client
现在我很难用UDP数据包实现这个功能。这是我的密码:
客户:
服务器:
我建议查看UDP协议(),然后使用一些基本示例。有很多关于UDP和Java的示例(例如)我建议先查看UDP协议(),然后使用一些基本示例。UDP和Java有很多例子(例如)我同意Daniel H。你也应该特别注意。我同意Daniel H。你也应该特别注意。我想知道你是否混淆了TCP和UDP?你的代码在类名中引用了TCP,但你说的是UDP?这两种协议都使用IP,但具有不同的特点。可靠性/碎片化/复制等
请参阅以了解差异。我想知道您是否混淆了TCP和UDP?你的代码在类名中引用了TCP,但你说的是UDP?这两种协议都使用IP,但具有不同的特点。可靠性/碎片化/复制等
请参阅以了解差异。这当然可以使用UDP数据报完成。然而,由于UDP本身不提供可靠性或有序的数据包传递,这将有点困难。应用程序需要这些功能才能将文件传递到客户端。如果选择使用UDP,则需要编写额外的代码来完成此操作。你确定你真的要使用UDP吗 如果您像上面的示例一样选择TCP,您就不必担心字节是否以正确的顺序到达那里
首先,我将在中查看一些示例,这当然可以使用UDP数据报来完成。然而,由于UDP本身不提供可靠性或有序的数据包传递,这将有点困难。应用程序需要这些功能才能将文件传递到客户端。如果选择使用UDP,则需要编写额外的代码来完成此操作。你确定你真的要使用UDP吗 如果您像上面的示例一样选择TCP,您就不必担心字节是否以正确的顺序到达那里
首先,我将在中检查一些示例,我知道这两个示例都是不同的,示例是我在传递到udp用法而不是TCP时遇到问题的代码。我知道这两个示例都是不同的,示例是我在传递到udp用法而不是TCP时遇到问题的代码。确切地说,我需要的信息,我将阅读它。这是我一个朋友的java测试,tcp值10分中的7分,剩下的3分是UDP,我希望她得到10分:/正是我需要的信息,我会读的。这是我一个朋友的java测试,tcp值10分中的7分,其余3分是UDP,我希望她能得到10分:/Ésempre bom ver que o Portuguès dáa volta ao mundo=Dhaha,brasileiro pois,mas estáa valer。[]他是葡萄牙的一员,他是巴西的一员,他是葡萄牙的一员。[]的
package br.com.redes.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import br.com.redes.configuration.CommonKeys;
public class TCPClient {
public static void exibirCabecalho(){
System.out.println("-------------------------");
System.out.println("TCP CLIENT");
System.out.println("-------------------------");
}
public static void main(String[] args) throws IOException {
TCPClient.exibirCabecalho();
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
if (args.length != 1){
System.out.println("O Programa deve ser chamado pelo nome + nome do servidor");
System.out.println("Ex: java TCPClient localhost");
System.exit(1);
}
System.out.println("Conectando ao servidor...");
try {
echoSocket = new Socket( args[0] , CommonKeys.PORTA_SERVIDOR);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket
.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Host não encontrado (" + args[0] + ")");
System.exit(1);
} catch (IOException e) {
System.err.println("Erro ao inicializar I/O para conexao");
System.exit(1);
}
System.out.println("Conectado, digite 'sair' sem as aspas para finalizar");
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
String inputLine = in.readLine();
if (inputLine == null){
System.out.println("Servidor terminou a conexão.");
System.out.println("Saindo...");
break;
}
System.out.println("Servidor: " + inputLine.replace("\\n", "\n"));
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
package br.com.redes.server;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import br.com.redes.configuration.CommonKeys;
public class TCPServer {
public static void exibirCabecalho(){
System.out.println("-------------------------");
System.out.println("TCP SERVER");
System.out.println("-------------------------");
}
public static void main(String[] args) throws IOException {
TCPServer.exibirCabecalho();
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket( CommonKeys.PORTA_SERVIDOR );
} catch (IOException e) {
System.err.println("Erro ao iniciar servidor na porta: " + CommonKeys.PORTA_SERVIDOR );
System.exit(1);
}
System.out.println("Iniciando servidor na porta: " + CommonKeys.PORTA_SERVIDOR);
System.out.println("Aguardando cliente...");
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Erro ao receber conexoes.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
System.out.println("Cliente conectado, aguardando caminhos pra leitura...");
while ((inputLine = in.readLine()) != null) {
if (inputLine.equalsIgnoreCase("sair")) {
System.out.println("Sair detectado, fechando servidor...");
break;
}
outputLine = processar(inputLine);
out.println(outputLine);
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
private static String processar(String inputLine) {
final String ARQUIVO_NAO_ENCONTRADO = "arquivo não encontrado.";
final String ARQUIVO_IO = "erro ao ler arquivo.";
System.out.println("Procurando arquivo: " + inputLine);
File f = new File(inputLine);
try {
BufferedReader input = new BufferedReader(new FileReader(f));
String linha = null;
StringBuffer retorno = new StringBuffer();
retorno.append("\\n");
retorno.append("Arquivo encontrado, lendo conteudo: " + inputLine + "\\n");
while (( linha = input.readLine()) != null){
retorno.append(linha + "\\n");
}
retorno.append("fim da leitura do arquivo\\n");
return retorno.toString();
} catch (FileNotFoundException e) {
return ARQUIVO_NAO_ENCONTRADO;
} catch (IOException e) {
return ARQUIVO_IO;
}
}
}