Sí, tengo el resto de código tanto del clientes su form y del servidor también. Solo pasé le código de servidorsocket:
Voy a pasar todo el código que tengo---
codigo de sockest-servidor y su formu1
/*COMUNICACION UDP (protocolo sin conexión/IP) CLIENTE-SERVIDOR CON HILOS
La conexión UDP no es orientada a conexión, es decir, el servidor en principio
no sabe de donde viene el mensaje, aunque lo puede saber mirando el datagrama que le llega.
En otras palabras y comparando con la conexión TCP, UDP no necesita saber quien se conecta a él.
En UDP se envían datagramas a modo de mensajes.*/
package sockets4_servidor;
import java.io.*;
import java.net.*;
public class Sockets4_servidor {
static formu1 formu = new formu1();
static Sockets4_servidor meto = new Sockets4_servidor();
int PUERTO = 5000; //Puerto servidor
String mensa1;
byte[] buffer = new byte[1024]; //buffer donde se almacenara los mensajes
DatagramSocket socketUDP;
DatagramPacket respuesta;
InetAddress direccion;
int puertoCliente;
public static void main(String[] args) {
formu.setVisible(true);meto.conexion();meto.ejecutarconexion();}
public void ejecutarconexion() {
Thread hilo = new Thread(new Runnable() {
@Override
@SuppressWarnings("empty-statement")
public void run() {
while (true) {
try {mensaje();}
finally {;} } } } );
hilo.start();}
public void conexion() {
try {
formu1.jTextArea1.append("Esperando conexión entrante en el puerto: " + PUERTO+ "\n");
direccion = InetAddress.getLocalHost();
formu1.jTextArea1.append("Servidor IP: " + direccion + "\n");}
catch (IOException e) {System.err.println("Error "+e);} }
public void mensaje() {
try {
//Creacion del socket
socketUDP = new DatagramSocket(PUERTO);
//Preparo la respuesta
DatagramPacket peticion = new DatagramPacket(buffer, buffer.length);
//Recibo el datagrama
socketUDP.receive(peticion);
//Convierto lo recibido y mostrar el mensaje
mensa1 =new String(peticion.getData());
formu1.jTextArea1.append(mensa1+"\n");
//Obtengo el puerto y la direccion de origen
//Sino se quiere responder, no es necesario
puertoCliente = peticion.getPort();
direccion = peticion.getAddress();
mensa1="";
buffer = mensa1.getBytes();
//creo el datagrama
respuesta = new DatagramPacket(buffer, buffer.length, direccion, puertoCliente);
//Envio la información
socketUDP.send(respuesta); }
catch (IOException e) {System.err.println("Error "+e);} }
}
// código de formu-servidor
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sockets4_servidor;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Usuario
*/
public class formu1 extends javax.swing.JFrame {
Sockets4_servidor socke4 = new Sockets4_servidor();
/**
* Creates new form formu1
*/
public formu1() {
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() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("SERVIDOR");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(47, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 594, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(37, 37, 37))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(38, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
/**
* @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(formu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(formu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(formu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(formu1.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 formu1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
public static javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
//codigo sockest-cliente
/*COMUNICACION UDP (protocolo sin conexión/IP) CLIENTE-SERVIDOR CON HILOS
La conexión UDP no es orientada a conexión, es decir, el servidor en principio
no sabe de donde viene el mensaje, aunque lo puede saber mirando el datagrama que le llega.
En otras palabras y comparando con la conexión TCP, UDP no necesita saber quien se conecta a él.
En UDP se envían datagramas a modo de mensajes.*/
package sockets4_cliente;
import java.io.*;
import java.net.*;
public class Sockets4_cliente {
static formu1 formu = new formu1();
static Sockets4_cliente meto = new Sockets4_cliente();
String HOST = "localhost"; //Host servidor (IP o localhost)
int PUERTO = 5000; //Puerto servidor
String mensa1;
byte[] buffer = new byte[1024]; //buffer donde se almacenara los mensajes
InetAddress direccionServidor;
public static void main(String[] args) throws UnknownHostException {
formu.setVisible(true);meto.conexion();meto.ejecutarconexion();}
public void ejecutarconexion() {
Thread hilo = new Thread(new Runnable() {
@Override
@SuppressWarnings("empty-statement")
public void run() {
while (true) {
try {mensaje();}
finally {;} } } } );
hilo.start();}
public void conexion() throws UnknownHostException {
String nombrepc= InetAddress.getLocalHost().getHostName();
formu1.jTextArea1.append("Nombre PC= "+nombrepc +" conectado a "+HOST+"\n");
//Obtengo la localizacion de localhost
direccionServidor = InetAddress.getByName(HOST);
}
public void mensaje() {
try {
//Creo el socket de UDP
DatagramSocket socketUDP = new DatagramSocket();
//Convierto el mensaje a bytes
mensa1="Hola Cliente";
buffer = mensa1.getBytes();
//Creo un datagrama
DatagramPacket pregunta = new DatagramPacket(buffer, buffer.length, direccionServidor, PUERTO);
//Lo envio con send
socketUDP.send(pregunta);
//Preparo la respuesta
DatagramPacket peticion = new DatagramPacket(buffer, buffer.length);
//Recibo la respuesta
socketUDP.receive(peticion);
//Cojo los datos y lo muestro
mensa1 = new String(peticion.getData());
formu1.jTextArea1.append(mensa1+"\n"); }
catch (IOException e) {System.err.println("Error "+e);} }
}
//codigo de formu1-cliente
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sockets4_cliente;
/**
*
* @author Usuario
*/
public class formu1 extends javax.swing.JFrame {
Sockets4_cliente socke4 = new Sockets4_cliente();
/**
* Creates new form formu1
*/
public formu1() {
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() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("CLIENTE");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 499, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(62, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(50, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
/**
* @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(formu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(formu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(formu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(formu1.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 formu1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
public static javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
Eso es todo lo que tengo