Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Threads

Status
Not open for further replies.

johno77

Technical User
Nov 1, 2003
40
GB
Hello

I have created a class below which for the main part sets up a gui. The two bits of the code i am concerned about are below

Tcpdump td = new Tcpdump();

try
{
td.printOutput(text,text1);
}
catch(IOException e){
System.out.println("tcpdump err");}

and the code just below that that sets up a tcp socket - waits for connections and prints whatever is fed into the socket (text) to a textarea on the gui.

ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(9800);
System.out.println("Opened port 9800");
} catch (IOException e) {
System.err.println("Could not listen on port: 9800.");
System.exit(1);
}

Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}

try{
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String input;
while((input = in.readLine())!=null){text.append(input+"\n");}
}
catch(IOException e1){}

The problem i have got is two fold - first snippet of code (tcpdump and printoutput) is basically a java version of tcpdump - it sits in the background and prints everything coming off the nic. Because this is constantly running in the background the bit below this namely the creation of a tcp socket never executes. The socket code again is similar although it hangs while waiting for a client to connect. The problem i want to solve is how can i thread these two snippets of code. Also i the tcp socket currently waits for one connection - when it receives this i would like to be able to somehow deal with other connections - again another thread issue i guess- the problem is ive never really dealt with threads before and feel out of my depth. If anyone could point me in the way to go it would be appreciated. I would offer some duke dollars but sadly they ran out a while ago. I have posted the serverside1 code in its entierity below.

Many thanks

john






import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import jpcap.*;
import java.io.*;
import java.net.*;

public class Serverside1 extends JFrame implements ActionListener
{
private JFrame window;
private MenuBar bar;
private MenuItem start, stop, save, sixteen, ten, twelve, fourteen, about, exit, help_top, clear;
private Menu textsize, capture, view, help;
private JTextArea text,text1;
private Tcpdump td;
private Font sixteen_pt, ten_pt, twelve_pt, fourteen_pt;
private Color color;
private BorderLayout layout;
private JFileChooser fc = new JFileChooser();
private FileOutputStream out;
private PrintStream p;
private File file;
private BufferedWriter bw;
private DatagramPacket sendPacket, receivePacket;
private DatagramSocket socket;


public Serverside1()
{

window = new JFrame("Network Packet Sniffer");
window.setSize(700,500);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
layout = new BorderLayout(5,5);
window.getContentPane().setLayout(layout);
text = new JTextArea("",18, 24);
text1 = new JTextArea("",5,10);
Color color = new Color(0,0,0);
Color color1 = new Color(0,255,0);
text.setBackground(color);
text.setLineWrap(false);
text.setEditable(false);
text.setWrapStyleWord(true);
text1.setBackground(color);
text.setForeground(color1);
text1.setForeground(color1);
text1.setLineWrap(true);
text1.setEditable(false);
text1.setWrapStyleWord(true);
text1.append(" Captured Packets \n\n");
text1.append(" TCP\t0\n");
text1.append(" UDP\t0\n");
text1.append(" ARP\t0\n");
text1.append(" ICMP\t0\n");

JScrollPane scrollingResult = new JScrollPane(text);
window.getContentPane().add(scrollingResult, BorderLayout.NORTH);
JScrollPane scrollingResult1 = new JScrollPane(text1);
window.getContentPane().add(scrollingResult1, BorderLayout.WEST);
window.show();


sixteen_pt = new Font("TimesRoman", Font.PLAIN, 16);
ten_pt = new Font("TimesRoman", Font.PLAIN, 10);
twelve_pt = new Font("TimesRoman", Font.PLAIN, 12);
fourteen_pt = new Font("TimesRoman", Font.PLAIN, 14);
bar = new MenuBar();
window.setMenuBar(bar);
about = new MenuItem("About");
help_top = new MenuItem("Help Topics");
start = new MenuItem("Start");
stop = new MenuItem("Stop");
save = new MenuItem("Save");
exit = new MenuItem("Exit");
clear = new MenuItem("Clear Screen");
sixteen = new MenuItem("16 pt");
ten = new MenuItem("10 pt");
twelve = new MenuItem("12 pt");
fourteen = new MenuItem("14 pt");
textsize = new Menu("Text Size");
capture = new Menu("Capture");
view = new Menu("View");
help = new Menu("Help");
capture.add(start);
capture.add(stop);
capture.add(save);
capture.add(exit);
textsize.add(ten);
textsize.add(twelve);
textsize.add(fourteen);
textsize.add(sixteen);
view.add(clear);
view.add(textsize);
help.add(help_top);
help.add(about);
bar.add(capture);
bar.add(view);
bar.add(help);
save.addActionListener(this);
clear.addActionListener(this);
ten.addActionListener(this);
twelve.addActionListener(this);
sixteen.addActionListener(this);
fourteen.addActionListener(this);
about.addActionListener(this);
exit.addActionListener(this);
window.setVisible(true);


try{
socket = new DatagramSocket(9876);
System.out.println("opened port 9876");
}
catch(SocketException se){
se.printStackTrace();
System.out.println("Couldn't open port");
System.exit(1);
}

Tcpdump td = new Tcpdump();

try
{
td.printOutput(text,text1);
}
catch(IOException e){
System.out.println("tcpdump err");}




ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(9800);
System.out.println("Opened port 9800");
} catch (IOException e) {
System.err.println("Could not listen on port: 9800.");
System.exit(1);
}

Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}

try{
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String input;
while((input = in.readLine())!=null){text.append(input+"\n");}
}
catch(IOException e1){}

}



public void actionPerformed(ActionEvent e){

if (e.getSource()==sixteen)
{
text.setFont(sixteen_pt);
text.repaint();
}
else if (e.getSource()==ten)
{
text.setFont(ten_pt);
text.repaint();
}
else if (e.getSource()==twelve)
{
text.setFont(twelve_pt);
text.repaint();
}
else if (e.getSource()==fourteen)
{
text.setFont(fourteen_pt);
text.repaint();
}
else if (e.getSource()==clear)
{
text.setText("");
}
else if (e.getSource()==save)
{
int returnVal = fc.showSaveDialog(Serverside1.this);
File file = null;
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
}

try
{
out = new FileOutputStream(file);
p = new PrintStream( out );
p.println (text.getText());
p.close();
}
catch (Exception f)
{System.err.println ("Error writing to file");}
}

else if(e.getSource()==about)
{
JOptionPane.showMessageDialog(window,
&quot;TCP/IP Simulator Version 1.0 2003\nCreated by John O'Meara <omearajohn@hotmail.com>&quot;);
}
else if(e.getSource()==exit)
{System.exit(0);}

}


}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top