Socket Closing and JVM_Bind error! All my code is here
843811Jan 30 2002 — edited Feb 2 2002Hi, I'm trying to create simple Client/Server Apps in which the Client sends the JButton that was clicked to the server and the server outputs to its JTextField and to the Serial Port, but i keep getting a JVM_Bind error, so i know im not closing the ports right... one other thing, in the command line i have to type >javaw servername.java TWICE in order for it to go, i seriously think the problem is with me not closing the ports right.
Thanks
CLIENT
++++++
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.security.*;
public class doss extends JFrame implements ActionListener{
JPanel jpan;
JTextField jtf0= new JTextField("textfield");
JButton jb0= new JButton("B0");
JButton jb1= new JButton("B1");
JButton jb2= new JButton("B2");
JButton jb3= new JButton("B3");
JButton jb4= new JButton("B4");
JButton jb5= new JButton("B5");
JButton jb6= new JButton("B6");
JButton jb7= new JButton("B7");
JButton jb8= new JButton("B8");
JButton jb9= new JButton("B9");
public doss(){
Container cPane = getContentPane();
jpan = new JPanel();
cPane.add(jpan);
jpan.add(jtf0);
jpan.add(jb0);
jpan.add(jb1);
jpan.add(jb2);
jpan.add(jb3);
jpan.add(jb4);
jpan.add(jb5);
jpan.add(jb6);
jpan.add(jb7);
jpan.add(jb8);
jpan.add(jb9);
jb0.setActionCommand("button0");jb0.addActionListener(this);
jb1.setActionCommand("button1");jb1.addActionListener(this);
jb2.setActionCommand("button2");jb2.addActionListener(this);
jb3.setActionCommand("button3");jb3.addActionListener(this);
jb4.setActionCommand("button4");jb4.addActionListener(this);
jb5.setActionCommand("button5");jb5.addActionListener(this);
jb6.setActionCommand("button6");jb6.addActionListener(this);
jb7.setActionCommand("button7");jb7.addActionListener(this);
jb8.setActionCommand("button8");jb8.addActionListener(this);
jb9.setActionCommand("button9");jb9.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
String command = event.getActionCommand();String myByteStr;
if(command.equals("button0")){
try{Socket newSock = new Socket("127.0.0.1",8090);
OutputStream out = newSock.getOutputStream();
myByteStr="0";byte outByte[]=myByteStr.getBytes();out.write(outByte);
newSock.close();}
catch(Exception e){}
jtf0.setText("Button 0 was Pushed");}
if(command.equals("button1")){
try{Socket newSock = new Socket("127.0.0.1",8090);
OutputStream out = newSock.getOutputStream();
myByteStr="1";byte outByte[]=myByteStr.getBytes();out.write(outByte);
newSock.close();}
catch(Exception e){jtf0.setText("Error: " + e);}
jtf0.setText("Button 1 was Pushed");}
if(command.equals("button2")){
try{Socket newSock = new Socket("127.0.0.1",8090);
OutputStream out = newSock.getOutputStream();
myByteStr="2";byte outByte[]=myByteStr.getBytes();out.write(outByte);
newSock.close();}
catch(Exception e){}
jtf0.setText("Button 2 was Pushed");}
if(command.equals("button3")){
try{Socket newSock = new Socket("127.0.0.1",8090);
OutputStream out = newSock.getOutputStream();
myByteStr="3";byte outByte[]=myByteStr.getBytes();out.write(outByte);
newSock.close();}
catch(Exception e){}
jtf0.setText("Button 3 was Pushed");}
if(command.equals("button4")){
try{Socket newSock = new Socket("127.0.0.1",8090);
OutputStream out = newSock.getOutputStream();
myByteStr="4";byte outByte[]=myByteStr.getBytes();out.write(outByte);
newSock.close();}
catch(Exception e){}
jtf0.setText("Button 4 was Pushed");}
if(command.equals("button5")){
try{Socket newSock = new Socket("127.0.0.1",8090);
OutputStream out = newSock.getOutputStream();
myByteStr="5";byte outByte[]=myByteStr.getBytes();out.write(outByte);
newSock.close();}
catch(Exception e){}
jtf0.setText("Button 5 was Pushed");}
if(command.equals("button6")){
try{Socket newSock = new Socket("127.0.0.1",8090);
OutputStream out = newSock.getOutputStream();
myByteStr="6";byte outByte[]=myByteStr.getBytes();out.write(outByte);
newSock.close();}
catch(Exception e){}
jtf0.setText("Button 6 was Pushed");}
if(command.equals("button7")){
try{Socket newSock = new Socket("127.0.0.1",8090);
OutputStream out = newSock.getOutputStream();
myByteStr="7";byte outByte[]=myByteStr.getBytes();out.write(outByte);
newSock.close();}
catch(Exception e){}
jtf0.setText("Button 7 was Pushed");}
if(command.equals("button8")){
try{Socket newSock = new Socket("127.0.0.1",8090);
OutputStream out = newSock.getOutputStream();
myByteStr="8";byte outByte[]=myByteStr.getBytes();out.write(outByte);
newSock.close();}
catch(Exception e){}
jtf0.setText("Button 8 was Pushed");
}
if(command.equals("button9")){
try{Socket newSock = new Socket("127.0.0.1",8090);
OutputStream out = newSock.getOutputStream();
myByteStr="9";byte outByte[]=myByteStr.getBytes();out.write(outByte);
newSock.close();}
catch(Exception e){}
jtf0.setText("Button 9 was Pushed");}
}
public static void main(String args[]){
JFrame af = new doss();
af.setBounds(200,200,350,350);
af.setVisible(true);
}
}
+++++++
SERVER
+++++++
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.security.*;
import javax.comm.*;
import java.util.*;
import java.lang.String;
public class dossServ extends JFrame {
JPanel jpan;
JTextField jtf0= new JTextField("textfield");
public dossServ(){
Container cPane = getContentPane();
jpan = new JPanel();
cPane.add(jpan);
jpan.add(jtf0);
final int BAUD = 9600; PrintStream outStream;
SerialPort sPort; CommPort cPort;
CommPortIdentifier cpIdent;
Enumeration pList = CommPortIdentifier.getPortIdentifiers();
String one="1 Recieved and Sent";
String two="2 Recieved and Sent";
String tre="3 Recieved and Sent";
String fur="4 Recieved and Sent";
String fiv="5 Recieved and Sent";
String six="6 Recieved and Sent";
String sev="7 Recieved and Sent";
String eit="8 Recieved and Sent";
String nin="9 Recieved and Sent";
ServerSocket socket = null;
Socket insock = null;
BufferedReader inRead = null;
String str=" ";jtf0.setText("boo");
cpIdent = (CommPortIdentifier)pList.nextElement();
jtf0.setText("boo2");
try{socket = new ServerSocket(8090);}
catch(Exception e1){jtf0.setText(e1.toString());return;}
if(cpIdent.getPortType()==CommPortIdentifier.PORT_SERIAL)
jtf0.setText(cpIdent.getName());
else
jtf0.setText("NULL");
while(true){
try{insock = socket.accept();
inRead = new BufferedReader(new InputStreamReader(insock.getInputStream()));
str = inRead.readLine();}
catch(Exception e){System.out.println(e);return;}
try{cPort = cpIdent.open("COM1",5000);
sPort = (SerialPort)cPort;
try{sPort.setSerialPortParams(BAUD,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_EVEN);
try{
if(str.equals("1")){jtf0.setText(one);
outStream = new PrintStream(sPort.getOutputStream(), true);
outStream.write(0x01);outStream.close();}
if(str.equals("2")){jtf0.setText(two);
outStream = new PrintStream(sPort.getOutputStream(), true);
outStream.write(0x02); outStream.close();}
if(str.equals("3")){jtf0.setText(tre);
outStream = new PrintStream(sPort.getOutputStream(), true);
outStream.write(0x03); outStream.close();}
if(str.equals("4")){jtf0.setText(fur);
outStream = new PrintStream(sPort.getOutputStream(), true);
outStream.write(0x04); outStream.close();}
if(str.equals("5")){jtf0.setText(fiv);
outStream = new PrintStream(sPort.getOutputStream(), true);
outStream.write(0x05); outStream.close();}
if(str.equals("6")){jtf0.setText(six);
outStream = new PrintStream(sPort.getOutputStream(), true);
outStream.write(0x06); outStream.close();}
if(str.equals("7")){jtf0.setText(sev);
outStream = new PrintStream(sPort.getOutputStream(), true);
outStream.write(0x07); outStream.close();}
if(str.equals("8")){jtf0.setText(eit);
outStream = new PrintStream(sPort.getOutputStream(), true);
outStream.write(0x08); outStream.close();}
if(str.equals("9")){jtf0.setText(nin);
outStream = new PrintStream(sPort.getOutputStream(), true);
outStream.write(0x09); outStream.close();}}
catch(IOException ioex){};}
catch(UnsupportedCommOperationException ucoe){};}
catch(PortInUseException piue){};
}
}
public static void main (String args[]) throws Exception,IOException{
JFrame af = new dossServ();
af.setBounds(200,200,350,350);
af.setVisible(true);
}
}