Com UDP , eu não preciso pegar o tamanho da mensagem né ? Estou com um pouco de dificuldade pra pegar o tamanho da mensagem!
Vou tentar transportar a palavra “JAVA” e ver o que acontece… em TCP mesmo!
Entao Viny, eu li seu tópico mas estou com dúvida nesta parte:
[quote]Para cada arquivo
1 int - Tamanho da string com o nome do diretório
String - Com o nome do diretório.
[/quote]
Tamanho da string = OK , mas por que string + nome do diretório? Não entendo isto de nome do diretório!
String - com nome do diretório???
Vou tentar ver uns videos no youtube enquanto isso!, qualquer coisa eu posto novamente!
Ok gente, estou tentando usar printStream mas esta dando erro! Preciso de ajuda!
Quero comecar com algo bem simples pra comecar a aprender TCP e UDP depois!
package sendreceive;
import java.io.BufferedReader;
import java.io.*;
import java.net.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class Receiver {
private static BufferedReader SS_BF;
public static Socket socket;
public static void main() throws IOException
{
ServerSocket socket = new ServerSocket(9999);
//Esse código abaixo faz travar o programa!
Socket SS_Accept = socket.accept();
//Eu acho que preciso de um método que substitua esse .accept();
//Esse aqui não!
SS_BF = new BufferedReader(new InputStreamReader(SS_Accept.getInputStream()));
}
public static void Reader() throws IOException
{
try {
String temp = SS_BF.readLine();
} catch (IOException ex) {
System.out.println(ex.getStackTrace());
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
}
package sendreceive;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
public class Transport {
public static String Stringa;
public static String Stringb;
public static int Intc;
public static byte[] buffera;
public static int Inti;
public static int Intii;
public static Socket socketCliente;
public static BufferedReader SS_BF;
DataOutputStream output = null;
DataInputStream input = null;
public static void main() throws IOException
{
socketCliente = new Socket("localhost",9999);
Stringa = "Ola";
Stringb = "Número";
Intc = 5;
send(Stringa);
}
public static void send(String a) throws IOException
{
PrintStream sender = new PrintStream(socketCliente.getOutputStream());
sender.println(a);
}
}
Agora a interface :
package sendreceive;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class InterfaceA extends javax.swing.JFrame {
/** Creates new form InterfaceA */
public InterfaceA() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Iniciar Servidor");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Iniciar Cliente");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Ler Mensagem");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
.addComponent(jButton2))
.addContainerGap(166, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(18, 18, 18)
.addComponent(jButton3)
.addContainerGap(212, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Receiver.main();
} catch (IOException ex) {
System.out.println(ex.getStackTrace());
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Receiver.Reader();
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Transport.main();
} catch (IOException ex) {
System.out.println(ex.getStackTrace());
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(InterfaceA.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(InterfaceA.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(InterfaceA.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(InterfaceA.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InterfaceA().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
// End of variables declaration
}
O nome do diretório era uma coisa específica do autor do tópico onde expliquei sobre protocolo.
Era o que eu estava explicando no outro tópico, não basta simplesmente sair usando um PrintStream em cima de um socket.
No seu caso, seria talvez um campo inteiro com o id do autor, outro com o tamanho da mensagem e a mensagem em si.