Estou com o seguinte problema eu fiz um servidor de soquet para receber informacoes via socket mas esta me retornando a exception :(:
mas isso apos varias conexoes…
java.io.UTFDataFormatException
at java.io.DataOutputStream.writeUTF(DataOutputStream.java:342)
at java.io.RandomAccessFile.writeUTF(RandomAccessFile.java:1041)
at SocketServidor.Arquivo.escreverArquivo(Arquivo.java:70)
at SocketServidor.Servidor$1.run(Servidor.java:42)
Eu chama o servidor da seguinte forma:
public class SocketCliente {
public SocketCliente() {
}
public String escrever(String value){
String mens = "";
try {
// protocolo://ip:port
Socket socket = new Socket("localhost",9090);
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(value);
mens = in.readUTF();
in.close();
out.close();
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return mens;
}
}
public class SocketCliente {
public SocketCliente() {
}
public String escrever(String value){
String mens = "";
try {
// protocolo://ip:port
Socket socket = new Socket("localhost",9090);
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(value);
mens = in.readUTF();
in.close();
out.close();
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return mens;
}
}
Abaixo as duas classes do meu servidor
package SocketServidor;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.text.html.parser.Parser;
public class Servidor {
/** Creates a new instance of Servidor */
public Servidor() {
}
public static void main(String[] args){
try {
ServerSocket ss = new ServerSocket(9090);
System.out.print("Server running on 9090...\n");
int cont = 0;
while (true){
final Socket socket = ss.accept();
System.out.print("Equipamento conectou [ ");
cont++;
System.out.print(cont);
System.out.print(" ] \n");
new Thread(){
public void run(){
Arquivo arq = new Arquivo(socket);
try {
arq.criarArquivo("dados.txt", false);
arq.escreverArquivo();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
arq.fecharArquivo();
}
}
}.start();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
package SocketServidor;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.text.html.parser.Parser;
public class Servidor {
/** Creates a new instance of Servidor */
public Servidor() {
}
public static void main(String[] args){
try {
ServerSocket ss = new ServerSocket(9090);
System.out.print("Server running on 9090...\n");
int cont = 0;
while (true){
final Socket socket = ss.accept();
System.out.print("Equipamento conectou [ ");
cont++;
System.out.print(cont);
System.out.print(" ] \n");
new Thread(){
public void run(){
Arquivo arq = new Arquivo(socket);
try {
arq.criarArquivo("dados.txt", false);
arq.escreverArquivo();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
arq.fecharArquivo();
}
}
}.start();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
package SocketServidor;
import java.io.*;
import java.net.Socket;
import java.nio.Buffer;
public class Arquivo
{
String caminho;
private File file;
private PrintStream printer;
private DataInputStream in;
private final Socket _socket;
/** Creates a new instance of Arquivo */
public Arquivo(Socket socket) {
_socket = socket;
}
public void criarArquivo(String caminho, boolean novo) throws IOException
{
file = new File(caminho);
//Cria o arquivo caso ele não exista
if(!this.file.exists())
{
if (this.file.createNewFile())
System.out.println("Arquivo Criado com Sucesso!\n");
else
System.out.println("Falha ao criar o arquivo\n");
}
else if(novo)
{
this.file.delete();
this.file.createNewFile();
}
}
public void escreverArquivo() throws IOException
{
String texto = "";
DataInputStream in = new DataInputStream(_socket.getInputStream());
DataOutputStream out = new DataOutputStream(_socket.getOutputStream());
texto = in.readUTF() + "\n";
RandomAccessFile raf = new RandomAccessFile(file, "rw");
String temp = this.lerArquivo();
if (temp.length() > 0 )
texto = temp + texto;
raf.writeUTF(texto);
raf.close();
out.writeUTF("OK!");
in.close();
out.close();
_socket.close();
}
public String lerArquivo() throws IOException
{
this.in = new DataInputStream(new FileInputStream(file));
String texto = "";
while(in.available() > 0)
{
int rcv = in.read();
texto = texto + (char)rcv;
}
in.close();
return texto;
}
public void fecharArquivo()
{
//BufferedWriter a= new BufferedWriter();
}
}