Nombre: Laboratorio N 1 Laboratorio Sockets Funcion Servidor
Nombre: Laboratorio N 1 Laboratorio Sockets Funcion Servidor
LABORATORIO N 1
Laboratorio Sockets
Funcion Servidor
/*
*/
package laboratorio_sockets;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author Once
*/
/**
*/
static Socket s;
public servidor() {
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
*/
@SuppressWarnings("unchecked")
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Servidor");
msg_area.setColumns(20);
msg_area.setRows(5);
jScrollPane1.setViewportView(msg_area);
msg_text.addActionListener(new java.awt.event.ActionListener() {
msg_textActionPerformed(evt);
});
msg_send.setText("Responder");
msg_send.addActionListener(new java.awt.event.ActionListener() {
msg_sendActionPerformed(evt);
});
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37,
Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addContainerGap(27, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
try{
String msgout="";
msgout=msg_text.getText().trim();
dout.writeUTF( msgout);
}catch(Exception e){
}
/**
*/
//<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.
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
java.util.logging.Logger.getLogger(servidor.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(servidor.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(servidor.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(servidor.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new servidor().setVisible(true);
});
String msgin="";
try{
ss = new ServerSocket(1201);
s = ss.accept();
while(!msgin.equals("exit")){
msgin= din.readUTF();
msg_area.setText(msg_area.getText().trim()+"\n"+msgin);
}catch(Exception e){
}
Funcion Cliente:
/*
*/
package laboratorio_sockets;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
/**
* @author Once
*/
/**
*/
static Socket s;
public cliente() {
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
*/
@SuppressWarnings("unchecked")
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Cliente");
msg_area.setColumns(20);
msg_area.setRows(5);
jScrollPane1.setViewportView(msg_area);
msg_send.setText("Enviar");
msg_send.addActionListener(new java.awt.event.ActionListener() {
msg_sendActionPerformed(evt);
});
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(msg_text)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void msg_sendActionPerformed(java.awt.event.ActionEvent evt) {
try{
String msgout="";
msgout = msg_text.getText().trim();
dout.writeUTF(msgout);
}catch(Exception e){
/**
*/
//<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.
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
java.util.logging.Logger.getLogger(cliente.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(cliente.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
java.util.logging.Logger.getLogger(cliente.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new cliente().setVisible(true);
});
try{
String msgin="";
while(!msgin.equals("exit")){
msgin = din.readUTF();
msg_area.setText(msg_area.getText().trim()+"\n Server\t"+msgin);
}
}catch(Exception e){