hello friends,
i am new to networking ...
i have made code to make chatting from client to server and server to client but its not working
when i pressed send button button remain pressed and i can't send anymore msg
developed in NetBeans 5.0
here is the code
====================
SERVER code
====================
/*
* ServerSock.java
*
* Created on October 11, 2006, 11:21 AM
*/
package server;
import java.io.*;
import java.awt.*;
import java.net.*;
import java.util.*;
/**
*
* @author admin
*/
public class ServerSock extends javax.swing.JFrame
{
int port = 5050;
BufferedReader br ;
BufferedWriter bw;
String str;
/** Creates new form ServerSock */
public ServerSock()
{
super("SERVER");
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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
toSend = new javax.swing.JTextField();
ta = new javax.swing.JScrollPane();
ta2 = new javax.swing.JTextArea();
jLabel3 = new javax.swing.JLabel();
sendButton = new javax.swing.JButton();
statusLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
jLabel1.setText("SERVER");
jLabel2.setText("Message to sent :");
ta2.setColumns(20);
ta2.setEditable(false);
ta2.setRows(5);
ta.setViewportView(ta2);
jLabel3.setText("Received msg :");
sendButton.setText("Send");
statusLabel.setText("[ Ready ]");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(sendButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 83, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(24, 24, 24)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, statusLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(toSend)
.add(ta, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE))))))
.add(layout.createSequentialGroup()
.add(175, 175, 175)
.add(jLabel1)))
.addContainerGap(51, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.add(27, 27, 27)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(ta, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel3))
.add(20, 20, 20)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(toSend, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel2))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(sendButton)
.add(13, 13, 13)
.add(statusLabel)
.addContainerGap(22, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public void getConnected()
{
// getting connected with client ( listen client )
System.out.println("Get connected called inside");
try
{
// making try
ServerSocket serverSocket = new ServerSocket(port);
statusLabel.setText("Listening on port : ["+port+"]");
while(true)
{
//continuous listening
Socket clientSocket = serverSocket.accept();
statusLabel.setText("Connected on :"+ clientSocket.getRemoteSocketAddress());
br = new BufferedReader(new InputStreamReader (clientSocket.getInputStream()));
String line=toSend.getText();
do
{
line = br.readLine();
if(line !=null)
ta2.setText(line);
}while(line.equals("bye"));
}
}catch(Exception e) { System.out.println(e);}
}
public static void main(String args[])
{
ServerSock ss = new ServerSock();
ss.setVisible(true);
ss.statusLabel.setForeground(Color.RED);
ss.getConnected();
System.out.println("Get connected called");
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JButton sendButton;
private javax.swing.JLabel statusLabel;
private javax.swing.JScrollPane ta;
private javax.swing.JTextArea ta2;
private javax.swing.JTextField toSend;
// End of variables declaration
}
==================
CLIENT CODE
=======================================
/*
* ClientSock.java
*
* Created on October 11, 2006, 11:55 AM
*/
package client;
import java.awt.Color;
import java.net.*;
import java.io.*;
import java.util.Date;
/**
*
* @author admin
*/
public class ClientSock extends javax.swing.JFrame {
/** Creates new form ClientSock */
int port = 5050;
Socket client;
PrintWriter out = null;
BufferedReader networkIn = null;
public ClientSock()
{
super("CLIENT");
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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
ta = new javax.swing.JTextArea();
toSent = new javax.swing.JTextField();
sendButton = new javax.swing.JButton();
StatusLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("CLIENT");
ta.setColumns(20);
ta.setRows(5);
jScrollPane1.setViewportView(ta);
toSent.setText("<sent to server>");
sendButton.setText("Send");
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendButtonActionPerformed(evt);
}
});
StatusLabel.setText("< ready >");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(163, 163, 163)
.add(jLabel1))
.add(layout.createSequentialGroup()
.add(55, 55, 55)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(toSent)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, sendButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(StatusLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap(87, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(22, 22, 22)
.add(jLabel1)
.add(20, 20, 20)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(23, 23, 23)
.add(toSent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(14, 14, 14)
.add(sendButton)
.add(18, 18, 18)
.add(StatusLabel)
.addContainerGap(37, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String line= toSent.getText();
ta.setText(line);
try {
networkIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
//BufferedReader userIn = new BufferedReader(new InputStreamReader(cl));
out = new PrintWriter(client.getOutputStream());
System.out.println("Connected to echo server");
String theLine = line;
System.out.println("Inside while");
out.println(theLine);
out.flush();
System.out.println("before hanged");
//System.out.println(networkIn.readLine());
} // end try
catch (IOException e) {
System.err.println(e);
}
}
/**
* @param args the command line arguments
*/
public void getConnected()
{
try
{
//trying
StatusLabel.setText("Trying to Connect on Server");
client = new Socket("192.168.1.12",port);
StatusLabel.setText("Connected to server on :"+client.getRemoteSocketAddress());
}catch(Exception e) { System.out.println(e); }
}
public static void main(String args[])
{
ClientSock cs = new ClientSock();
cs.setVisible(true);
cs.StatusLabel.setForeground(Color.RED);
cs.getConnected();
}
// Variables declaration - do not modify
private javax.swing.JLabel StatusLabel;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton sendButton;
private javax.swing.JTextArea ta;
private javax.swing.JTextField toSent;
// End of variables declaration
}
if is there any running code in SWING for both client and server for chatting its better for me to understand how it works...
acutally later on i wanna to develop file transfer program using sockets over LAN.
please reply me back
thanks