java.net.SocketException: socket closed

I get this error when i try to read text from the server, My code is as follows

package VChat;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.InetAddress;

import javax.swing.JOptionPane;

import java.net.*;

import java.sql.*;

public class frmInvite extends javax.swing.JFrame

{

InetAddress address = null;

InputStream in;

Socket socket = null;

Thread reader,writer;

public static final int DEFAULT_PORT = 12345;

public frmInvite()

{

initComponents();

frmVoice objVoice = new frmVoice();

System.out.println(objVoice.str_Usr);

txtUsr.setText(objVoice.str_Usr);

setVisible(true);

CenterScreen objCenter = new CenterScreen();

objCenter.setCenterScreen(this);

reader = new Reader(this);

writer = new Writer(this);

}

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

private void initComponents() {

jPanel1 = new javax.swing.JPanel();

pnlMain = new javax.swing.JPanel();

jLabel1 = new javax.swing.JLabel();

txtUsr = new javax.swing.JTextField();

btnConfirm = new javax.swing.JButton();

btnCancel = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

setTitle("Invitation To Your Buddy");

setResizable(false);

jPanel1.setBackground(java.awt.SystemColor.desktop);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

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

.addGap(0, 379, Short.MAX_VALUE)

);

jPanel1Layout.setVerticalGroup(

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

.addGap(0, 22, Short.MAX_VALUE)

);

pnlMain.setBorder(javax.swing.BorderFactory.createEtchedBorder());

jLabel1.setFont(new java.awt.Font("Verdana", 1, 12));

jLabel1.setForeground(new java.awt.Color(0, 0, 255));

jLabel1.setText("You Are Inviting To");

txtUsr.setFont(new java.awt.Font("Verdana", 1, 12));

btnConfirm.setFont(new java.awt.Font("Verdana", 1, 12));

btnConfirm.setForeground(new java.awt.Color(0, 0, 255));

btnConfirm.setText("Confirm!");

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

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

btnConfirmActionPerformed(evt);

}

});

btnCancel.setFont(new java.awt.Font("Verdana", 1, 12));

btnCancel.setForeground(new java.awt.Color(255, 0, 0));

btnCancel.setText("Cancel");

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

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

btnCancelActionPerformed(evt);

}

});

javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain);

pnlMain.setLayout(pnlMainLayout);

pnlMainLayout.setHorizontalGroup(

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

.addGroup(pnlMainLayout.createSequentialGroup()

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

.addGroup(pnlMainLayout.createSequentialGroup()

.addContainerGap()

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlMainLayout.createSequentialGroup()

.addContainerGap(77, Short.MAX_VALUE)

.addComponent(btnConfirm)

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

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

.addGroup(pnlMainLayout.createSequentialGroup()

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

.addComponent(txtUsr, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(pnlMainLayout.createSequentialGroup()

.addGap(30, 30, 30)

.addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))

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

);

pnlMainLayout.setVerticalGroup(

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

.addGroup(pnlMainLayout.createSequentialGroup()

.addContainerGap()

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

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(txtUsr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

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

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

.addComponent(btnConfirm)

.addComponent(btnCancel))

.addGap(25, 25, 25))

);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

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

.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGroup(layout.createSequentialGroup()

.addGap(10, 10, 10)

.addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGap(10, 10, 10))

);

layout.setVerticalGroup(

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

.addGroup(layout.createSequentialGroup()

.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

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

.addComponent(pnlMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

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

);

pack();

}// </editor-fold>

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

getIPInfo();

createSocket();

}

private void createSocket()

{

try

{

socket = new Socket(address,DEFAULT_PORT);

}

catch(IOException ex)

{

JOptionPane.showMessageDialog(null,ex.getMessage());

}

try

{

writer.setPriority(6);

writer.start();

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

JOptionPane.showMessageDialog(null,in.readLine());

//reader.setPriority(5)

//reader.start();

}

catch(Exception ex)

{

JOptionPane.showMessageDialog(null,"Error" + ex);

}

}

private void getIPInfo()

{

Connection objconn = null;

Statement objStmt = null;

ResultSet objResult = null;

try

{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

objconn = DriverManager.getConnection("jdbc:microsoft:sqlserver://nikunj;DatabaseName=wildfire;User=sa;Password=password");

objStmt = objconn.createStatement();

objResult = objStmt.executeQuery("Select * from userinfo where computerNm ='" + txtUsr.getText() +"'");

objResult.next();

address = InetAddress.getByName(objResult.getString(3));

}

catch(Exception ex)

{

JOptionPane.showMessageDialog(null,ex.getMessage());

}

}

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

// TODO add your handling code here:

this.setVisible(false);

}

/**

* @param args the command line arguments

*/

//public static void main(String args[]) {

//java.awt.EventQueue.invokeLater(new Runnable() {

//public void run() {

//new frmInvite().setVisible(true);

//}

//});

//}

// Variables declaration - do not modify

private javax.swing.JButton btnCancel;

private javax.swing.JButton btnConfirm;

private javax.swing.JLabel jLabel1;

private javax.swing.JPanel jPanel1;

private javax.swing.JPanel pnlMain;

private javax.swing.JTextField txtUsr;

// End of variables declaration

}

class Reader extends Thread

{

frmInvite objInvite;

public Reader(frmInvite objInvite)

{

super("Client Reader");

this.objInvite = objInvite;

}

public void run()

{

try

{

//InputStream in = objInvite.socket.getInputStream();

BufferedReader in = new BufferedReader(new InputStreamReader(objInvite.socket.getInputStream()));

JOptionPane.showMessageDialog(null,"Entered");

String line;

while(true)

{

line = in.readLine();

if(line.equals("Accept") || line.equals("Decline"))

{

frmAckSender objSender = new frmAckSender();

objSender.setVisible(true);

}

if(line == null)

{

JOptionPane.showMessageDialog(null,"Server Closed Connection");

break;

}

}

}

catch(Exception ex)

{

JOptionPane.showMessageDialog(null,"Reader Thread Error" + ex);

System.out.println(ex.getMessage());

}

}

}

class Writer extends Thread

{

frmInvite objInvite ;

public Writer(frmInvite obj)

{

super("Client Writer");

objInvite = obj;

}

public void run()

{

PrintWriter out = null;

String line;

try

{

out = new PrintWriter(objInvite.socket.getOutputStream(),true);

out.print("Invite");

out.close();

}

catch(Exception ex)

{

JOptionPane.showMessageDialog(null,"Writer Thread Error");

}

}

}

[10578 byte] By [Sunaya] at [2007-11-26 20:52:54]
# 1
The exception means that you have closed the socket yourself. Closing a socket input or output stream closes the other stream and the socket. You also have a problem with a possible NullPointerException where you test the result of readLine() against a String before you test it for null.
ejpa at 2007-7-10 2:18:42 > top of Java-index,Archived Forums,Socket Programming...