1 Attachment(s)
Socket Listening problem in Netbeans
i am making peer to peer chat application. the following code can successfully send messages to next client application but could not receive the messages from the client. the code is written in Netbeans.
the listening code is commented at the moment because the code goes stuck on that area. can anybody solve this problem or suggest me how to solve this issue. I am a new user of Netbean. i think listening code correct because it is working somewhere else but i think code placement is not correct. so can anybody please solve or suggest me this issue, I really need to solve it urgently. Thank you.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* ServerChat.java
*
* Created on May 3, 2010, 12:04:34 PM
*/
package serverchat;
import java.io.*;
import java.net.*;
import java.util.*;
/**
*
* @author Adeel
*/
public class ServerChat extends javax.swing.JFrame {
/** Creates new form ServerChat */
public ServerChat() {
initComponents();
try{
// s1.connect(new InetSocketAddress("192.168.10.222", 8080))
s = new ServerSocket(100);
s1=s.accept();
br = new BufferedReader(new InputStreamReader(s1.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));
bw.write("Hello");
bw.newLine();
bw.flush();
Thread th;
th = new Thread(); // may be new Thread (this);
th.start();
}catch(Exception e){}
}
/** 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();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton1.setText("Send");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Sign Out");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel1.setText("jLabel1");
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(32, 32, 32)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
.addGap(31, 31, 31))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(345, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(8, 8, 8)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String chatText;
String newline = "\n"; // for new line in jTextArea
chatText=jTextField1.getText();
jTextArea1.append(chatText + newline);
try{
bw.write(chatText);
bw.newLine();
bw.flush();
jTextField1.setText("");
}catch(Exception m){}
// end send button method
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {new ServerChat().setVisible(true);
/////////// HERE IS THE LISTENING CODE //////////////////////
// try{s.setSoTimeout(1);
// }catch(Exception e){}
// while (true)
// {
//// try{list.add(br.readLine());
// }catch (Exception h){}
// }
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
// my code variables
static ServerSocket s;
static Socket s1;
static BufferedReader br;
static BufferedWriter bw;
static List list;
}
waiting for reply.. thanks again