0% found this document useful (0 votes)
18 views12 pages

Nombre: Laboratorio N 1 Laboratorio Sockets Funcion Servidor

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

Nombre: Laboratorio N 1 Laboratorio Sockets Funcion Servidor

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Nombre : Ancasi Fernando Jose

LABORATORIO N 1

Laboratorio Sockets

Funcion Servidor

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this


template

*/

package laboratorio_sockets;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.net.ServerSocket;

import java.net.Socket;

/**

* @author Once

*/

public class servidor extends javax.swing.JFrame {

/**

* Creates new form servidor

*/

static ServerSocket ss;

static Socket s;

static DataInputStream din;

static DataOutputStream dout;

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

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jScrollPane1 = new javax.swing.JScrollPane();

msg_area = new javax.swing.JTextArea();

msg_text = new javax.swing.JTextField();

msg_send = new javax.swing.JButton();

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() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

msg_textActionPerformed(evt);

});
msg_send.setText("Responder");

msg_send.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

msg_sendActionPerformed(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()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jScrollPane1))

.addGroup(layout.createSequentialGroup()

.addGap(170, 170, 170)

.addComponent(jLabel1)

.addGap(0, 0, Short.MAX_VALUE)))

.addContainerGap())

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(msg_text, javax.swing.GroupLayout.PREFERRED_SIZE, 199,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37,
Short.MAX_VALUE)

.addComponent(msg_send, javax.swing.GroupLayout.PREFERRED_SIZE, 134,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(24, 24, 24))

);

layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(23, 23, 23)

.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(msg_text, javax.swing.GroupLayout.PREFERRED_SIZE, 95,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(msg_send, javax.swing.GroupLayout.PREFERRED_SIZE, 44,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap(27, Short.MAX_VALUE))

);

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){

private void msg_textActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}
/**

* @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(servidor.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(servidor.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(servidor.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(servidor.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 servidor().setVisible(true);

});

String msgin="";

try{

ss = new ServerSocket(1201);

s = ss.accept();

din = new DataInputStream(s.getInputStream());

dout = new DataOutputStream( s.getOutputStream());

while(!msgin.equals("exit")){

msgin= din.readUTF();

msg_area.setText(msg_area.getText().trim()+"\n"+msgin);

}catch(Exception e){

// Variables declaration - do not modify

private javax.swing.JLabel jLabel1;

private javax.swing.JScrollPane jScrollPane1;

private static javax.swing.JTextArea msg_area;

private javax.swing.JButton msg_send;

private javax.swing.JTextField msg_text;

// End of variables declaration

}
Funcion Cliente:

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this


template

*/

package laboratorio_sockets;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.net.Socket;

/**

* @author Once

*/

public class cliente extends javax.swing.JFrame {

/**

* Creates new form cliente

*/

static Socket s;

static DataInputStream din;

static DataOutputStream dout;

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

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jScrollPane1 = new javax.swing.JScrollPane();

msg_area = new javax.swing.JTextArea();

msg_text = new javax.swing.JTextField();

msg_send = new javax.swing.JButton();

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() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

msg_sendActionPerformed(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()

.addComponent(msg_text)

.addGap(18, 18, 18)

.addComponent(msg_send, javax.swing.GroupLayout.PREFERRED_SIZE, 114,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(24, 24, 24))

.addGroup(layout.createSequentialGroup()

.addGap(158, 158, 158)

.addComponent(jLabel1)

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400,


Short.MAX_VALUE)

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(14, 14, 14)

.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(msg_text, javax.swing.GroupLayout.DEFAULT_SIZE, 128,


Short.MAX_VALUE)

.addComponent(msg_send, javax.swing.GroupLayout.PREFERRED_SIZE, 47,


javax.swing.GroupLayout.PREFERRED_SIZE))

.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){

}// TODO add your handling code here:

/**

* @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(cliente.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(cliente.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(cliente.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(cliente.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 cliente().setVisible(true);

});

try{

s = new Socket("localhost", 1201);

din = new DataInputStream(s.getInputStream());

dout = new DataOutputStream(s.getOutputStream());

String msgin="";

while(!msgin.equals("exit")){

msgin = din.readUTF();

msg_area.setText(msg_area.getText().trim()+"\n Server\t"+msgin);

}
}catch(Exception e){

// Variables declaration - do not modify

private javax.swing.JLabel jLabel1;

private javax.swing.JScrollPane jScrollPane1;

private static javax.swing.JTextArea msg_area;

private javax.swing.JButton msg_send;

private javax.swing.JTextField msg_text;

// End of variables declaration

You might also like