diff options
Diffstat (limited to 'CS2452')
27 files changed, 1619 insertions, 0 deletions
diff --git a/CS2452/DictionarySearch.java b/CS2452/DictionarySearch.java new file mode 100644 index 0000000..5d86a91 --- /dev/null +++ b/CS2452/DictionarySearch.java @@ -0,0 +1,91 @@ +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class DictionarySearch +{ + public static String[] dict; + + public static void main(String[] args) + { + BufferedReader ir = new BufferedReader(new InputStreamReader(System.in)); + BufferedReader fr = null; + try + { + fr = new BufferedReader(new FileReader("/home/tje/workspace/Recursion/dictionary.wwf")); + } catch (FileNotFoundException e1) + { + e1.printStackTrace(); + } + + String str = null; + dict = new String[0]; + System.out.println("Dictionary loading..."); + + do + { + try + { + str = fr.readLine(); + } catch (IOException e) + { + e.printStackTrace(); + } + + String[] tmp = new String[dict.length + 1]; + int i; + for (i = 0; i < dict.length; i++ ) + { + tmp[i] = dict[i]; + } + tmp[i] = str; + dict = tmp; + + } while (str != null); + System.out.println("Dictionary loaded."); + + while (true) + { + System.out.print("What word would you like to look up? "); + try + { + System.out.println("The closest word is " + check(ir.readLine().toLowerCase(), 0, dict.length)); + } catch (IOException e) + { + e.printStackTrace(); + } + + + + } + } + + public static String check(String word, int a, int b) + { + if (a == b -1) + { + return dict[a]; + } + int mid = (a+b)/2; + if(word.compareTo(dict[mid]) > 0) + { + return check(word, mid, b); + } + + if (word.compareTo(dict[mid]) < 0) + { + return check(word, a, mid); + } + + if (word.compareTo(dict[mid]) == 0) + { + return dict[mid]; + } + + + return word; + + } +}
\ No newline at end of file diff --git a/CS2452/GUI_network/Gui.java b/CS2452/GUI_network/Gui.java new file mode 100644 index 0000000..2187030 --- /dev/null +++ b/CS2452/GUI_network/Gui.java @@ -0,0 +1,108 @@ +package assign6; + +import java.awt.*; +import java.awt.event.*; +import java.awt.image.*; +import java.io.*; +import java.util.concurrent.Semaphore; + +import javax.imageio.ImageIO; +import javax.swing.*; + +public class Gui extends JFrame implements ActionListener, MouseListener +{ + JPanel panel; + JLabel label; + BufferedImage image; + Color color; + Semaphore write; + boolean sc; + + + public Gui(String filename, Color color, Semaphore write, boolean sc) + { + this.color = color; + this.sc = sc; + + panel = new JPanel(new GridLayout(1,1)); + panel.setBackground(color); + try + { + image = ImageIO.read(new File(filename)); + } catch (IOException e) + { + e.printStackTrace(); + } + label = new JLabel(new ImageIcon(new ImageIcon(image).getImage().getScaledInstance(500, 500, Image.SCALE_DEFAULT))); + + panel.add(label); + panel.addMouseListener(this); + this.add(panel); + + this.write = write; + + } + + public void actionPerformed(ActionEvent e) + { + + } + + public void mouseClicked(MouseEvent e) + { + write.release(); + changeImage(); + } + + + public void mouseEntered(MouseEvent e) + { + + } + + public void mouseExited(MouseEvent e) + { + + } + + public void mousePressed(MouseEvent e) + { + + } + + public void mouseReleased(MouseEvent e) + { + + } + + public void changeImage() + { + + String filename; + Color color; + if(sc) + { + filename = "img2.jpg"; + color = Color.GREEN; + } + else + { + filename = "img1.jpg"; + color = Color.BLUE; + } + + try + { + image = ImageIO.read(new File(filename)); + } catch (IOException e) + { + e.printStackTrace(); + } + label.setIcon(new ImageIcon(new ImageIcon(image).getImage().getScaledInstance(500, 500, Image.SCALE_DEFAULT))); + + panel.setBackground(color); + panel.repaint(); + + sc = !sc; + } +}
\ No newline at end of file diff --git a/CS2452/GUI_network/MGui.java b/CS2452/GUI_network/MGui.java new file mode 100644 index 0000000..a0879c6 --- /dev/null +++ b/CS2452/GUI_network/MGui.java @@ -0,0 +1,92 @@ +package assign6; + +import java.net.*; +import java.util.concurrent.Semaphore; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +import java.io.*; + +public class MGui +{ + + public static void main(String[] args) + { + Gui g = null; + Semaphore write = new Semaphore(1); + try + { + write.acquire(); + } catch (InterruptedException e1) + { + e1.printStackTrace(); + } + ServerSocket ss; + Socket s = null; + + if (args[0].compareTo("server") == 0) + { + try + { + ss = new ServerSocket(Integer.parseInt(args[1])); + s = ss.accept(); + } catch (NumberFormatException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + g = new Gui("img1.jpg",Color.BLUE, write,true); + g.setSize(1000, 1000); + g.setLocation(32, 32); + g.setVisible(true); + g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + SocketWriter sw = new SocketWriter(s,write); + + + } + else if (args[0].compareTo("client") == 0) + { + + InetAddress address = null; + int port = 0; + + try + { + address = InetAddress.getByName(args[1]); + port = Integer.parseInt(args[2]); + + } catch (UnknownHostException e) + { + e.printStackTrace(); + } + + try + { + s = new Socket(address, port); + } catch (IOException e) + { + e.printStackTrace(); + } + + g = new Gui("img2.jpg",Color.GREEN,write,false); + g.setSize(1000, 1000); + g.setLocation(500, 500); + g.setVisible(true); + g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + SocketWriter sw = new SocketWriter(s,write); + + } + + SocketReader sr = new SocketReader(s,g); + + } + +} diff --git a/CS2452/GUI_network/SocketReader.java b/CS2452/GUI_network/SocketReader.java new file mode 100644 index 0000000..dacdaf1 --- /dev/null +++ b/CS2452/GUI_network/SocketReader.java @@ -0,0 +1,49 @@ +package assign6; + +import java.net.*; + +import javax.swing.JFrame; + +import java.io.*; + +public class SocketReader extends Thread +{ + BufferedReader br; + Gui g; + Thread t; + + public SocketReader(Socket s, Gui g) + { + try + { + br = new BufferedReader(new InputStreamReader(s.getInputStream())); + } catch (IOException e) + { + e.printStackTrace(); + } + + this.g = g; + + t = new Thread(this); + t.start(); + } + + public void run() + { + String str = null; + while (true) + { + try + { + str = br.readLine(); + } catch (IOException e) + { + e.printStackTrace(); + } + if (str != null) + g.changeImage(); + } + + } + +}
\ No newline at end of file diff --git a/CS2452/GUI_network/SocketWriter.java b/CS2452/GUI_network/SocketWriter.java new file mode 100644 index 0000000..1d882ab --- /dev/null +++ b/CS2452/GUI_network/SocketWriter.java @@ -0,0 +1,56 @@ +package assign6; + +import java.io.*; +import java.net.*; +import java.util.concurrent.Semaphore; + +public class SocketWriter extends Thread +{ + Thread t; + BufferedWriter bw; + Semaphore write; + boolean sc; + + public SocketWriter(Socket s, Semaphore write) + { + try{ + this.bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); + } + catch(IOException e) + { + e.printStackTrace(); + } + + this.write = write; + + + + t = new Thread(this); + t.start(); + } + + public void run() + { + + while(true) + { + try + { + write.acquire(); + } catch (InterruptedException e1) + { + e1.printStackTrace(); + } + try + { + bw.newLine(); + bw.flush(); + } catch (IOException e) + { + e.printStackTrace(); + } + } + + } + +} diff --git a/CS2452/Q8.java b/CS2452/Q8.java new file mode 100644 index 0000000..76c0bcb --- /dev/null +++ b/CS2452/Q8.java @@ -0,0 +1,33 @@ + +public class Q8 +{ + + public static void main(String[] args) + { + int[] str= new int[(int) Math.ceil(args[0].length()/6.00)]; + + args[0] = args[0].toLowerCase(); + + while (args[0].length() % 6 != 0) + { + args[0] = args[0].concat(String.format("%c",'a' + 31)); + } + + for(int i = 0; i < str.length; i++) + { + str[i] = 0; + for(int j = 0; j < 6; j++) + { + str[i] <<= 5; + str[i] += args[0].charAt(j + (i*6)) - 'a' ; + } + } + + for(int i = 0; i < str.length; i++) + { + System.out.printf("%x\n", str[i]); + } + + } + +} diff --git a/CS2452/Skype/src/Skype/Client.java b/CS2452/Skype/src/Skype/Client.java new file mode 100644 index 0000000..f1716fe --- /dev/null +++ b/CS2452/Skype/src/Skype/Client.java @@ -0,0 +1,63 @@ +package Skype; + +import java.io.IOException; +import java.net.*; +import java.util.concurrent.Semaphore; + +public class Client extends Thread +{ + Socket s; + InetAddress ip; + int port; + audio aud; + Gui g; + Semaphore audflg; + Semaphore guiflg; + + public Client(Gui g, String ip, String port, audio aud, Semaphore audflg,Semaphore guiflg) + { + this.aud = aud; + this.audflg = audflg; + this.guiflg = guiflg; + this.g = g; + + try + { + this.ip = InetAddress.getByName(ip); + } catch (UnknownHostException e) + { + e.printStackTrace(); + } + this.port = Integer.parseInt(port); + this.start(); + } + + public void run() + { + //System.out.printf("Client Started\n"); + try + { + s = new Socket(ip, port); + } catch (IOException e) + { + e.printStackTrace(); + } + try + { + audflg.acquire(); + } catch (InterruptedException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + aud.setOpen(true); + audflg.release(); + + speaker t1 = new speaker(s, audio.getFormat(), aud, audflg,g); + mic t2 = new mic(s, audio.getFormat(), g, guiflg); + + t1.start(); + t2.start(); + } + +} diff --git a/CS2452/Skype/src/Skype/Gui.java b/CS2452/Skype/src/Skype/Gui.java new file mode 100644 index 0000000..6e0f0a0 --- /dev/null +++ b/CS2452/Skype/src/Skype/Gui.java @@ -0,0 +1,222 @@ +package Skype; + +import java.awt.Color; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.concurrent.Semaphore; + +import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import javax.swing.text.AbstractDocument; + +public class Gui extends JFrame implements ActionListener, ChangeListener +{ + JButton call; + JCheckBox mute; + JSlider volume; + JLabel time, ipL, portL, volumeL,ipLH,portLH; + JTextField ipT, portT; + JPanel panel, ip, volumeP, slider; + GridBagConstraints c; + Timer timer; + int sec; + + boolean incall, servercall; + + Semaphore audflg; + Semaphore svrflg; + Semaphore guiflg; + audio aud; + + public Gui(audio aud, Semaphore svrflg, Semaphore audflg, Semaphore guiflg,int port) + { + this.guiflg = guiflg; + timer = new Timer(1000, this); + incall = false; + this.svrflg = svrflg; + this.audflg = audflg; + this.aud = aud; + + c = new GridBagConstraints(); + c.ipadx = 10; + c.ipady = 10; + + panel = new JPanel(new GridBagLayout()); + + call = new JButton("Call"); + call.addActionListener(this); + + //Volume Panel setup + volume = new JSlider(SwingConstants.VERTICAL); + volume.addChangeListener(this); + volume.setPaintTicks(true); + volume.setMajorTickSpacing(25); + volume.setMinorTickSpacing(5); + volume.setPaintTicks(true); + volume.setPaintLabels(true); + + volumeL = new JLabel("Volume"); + + volumeP = new JPanel(); + volumeP.setLayout(new GridBagLayout()); + + volumeP.add(volume, c); + c.gridx = 0; + c.gridy = 1; + volumeP.add(volumeL, c); + + mute = new JCheckBox("Mute"); + mute.addActionListener(this); + c.gridx = 1; + c.gridy = 1; + volumeP.add(mute, c); + volumeP.setVisible(true); + + //Address setup + ipL = new JLabel("IP:"); + ipT = new JTextField(15); + ipT.setText("127.0.0.1"); + ((AbstractDocument) ipT.getDocument()).setDocumentFilter(new numberFilter()); + ip = new JPanel(new GridBagLayout()); + c.gridx = 0; + c.gridy = 0; + c.anchor = GridBagConstraints.EAST; + ip.add(ipL, c); + c.gridx = 1; + + c.anchor = GridBagConstraints.WEST; + ip.add(ipT, c); + + portL = new JLabel("Port:"); + portT = new JTextField(5); + portT.setText("7777"); + ((AbstractDocument) portT.getDocument()).setDocumentFilter(new numberFilter()); + c.gridy = 1; + c.anchor = GridBagConstraints.WEST; + ip.add(portT, c); + c.gridx = 0; + c.anchor = GridBagConstraints.EAST; + ip.add(portL, c); + + try + { + ipLH = new JLabel("Your IP: ".concat(InetAddress.getLocalHost().getHostAddress().toString())); + } catch (UnknownHostException e) + { + e.printStackTrace(); + } + portLH = new JLabel("Your Port: ".concat(Integer.toString(port))); + + + c.anchor = GridBagConstraints.WEST; + c.gridy = 2; + c.gridx = 1; + ip.add(ipLH,c); + c.gridy++; + ip.add(portLH,c); + + time = new JLabel("TIME"); + + c.gridx = 0; + c.gridy = 0; + c.anchor = GridBagConstraints.NORTHWEST; + panel.add(ip, c); + c.gridx = 1; + panel.add(volumeP, c); + c.gridy = 1; + c.anchor = GridBagConstraints.CENTER; + panel.add(call, c); + c.gridx = 0; + panel.add(time, c); + panel.setVisible(true); + + this.add(panel); + + } + + public void actionPerformed(ActionEvent e) + { + try + { + guiflg.acquire(); + + if (e.getSource() == mute) + { + if (mute.isSelected()) + { + volume.setEnabled(false); + aud.setVolume(0); + } + else + { + volume.setEnabled(true); + aud.setVolume(volume.getValue()); + } + } + else if (e.getSource() == call) + { + if (incall) + { + call.setText("Call"); + timer.stop(); + //System.out.println("Hung up"); + incall = false; + try + { + audflg.acquire(); + } catch (InterruptedException e1) + { + e1.printStackTrace(); + } + //System.out.println("False"); + aud.setOpen(false); + audflg.release(); + svrflg.release(); //allow server to accept connections again. + } + else + { + call.setText("Hang up"); + sec = 0; + timer.start(); + Client c = new Client(this, ipT.getText(), portT.getText(), aud, audflg,guiflg); + //System.out.printf("IP: %s, Port: %s\n", ipT.getText(), portT.getText()); + incall = true; + + } + } + else if ((e.getSource() == timer) && incall) + { + sec++ ; + if (sec < 60 * 60) time.setText(String.format("%02d:%02d", sec / 60, sec % 60)); + else time.setText(String.format("%d:%02d:%02d", sec / (60 * 60), (sec / 60) % 60, sec % 60)); + } + } catch (InterruptedException e2) + { + // TODO Auto-generated catch block + e2.printStackTrace(); + } + guiflg.release(); + + } + + public void stateChanged(ChangeEvent e) + { + if (e.getSource() == volume) + { + if (audflg.tryAcquire()) + { + aud.setVolume(volume.getValue()); + //change volume .getValue(); + audflg.release(); + } + + } + } + +} diff --git a/CS2452/Skype/src/Skype/MGui.java b/CS2452/Skype/src/Skype/MGui.java new file mode 100644 index 0000000..c545ffe --- /dev/null +++ b/CS2452/Skype/src/Skype/MGui.java @@ -0,0 +1,27 @@ +package Skype; + +import java.util.concurrent.Semaphore; + +import javax.swing.JFrame; + +public class MGui +{ + public static void main(String[] args) + { + audio aud = new audio(50); + Semaphore volflg = new Semaphore(1); + Semaphore svrflg = new Semaphore(1); + Semaphore guiflg = new Semaphore(1); + + int port = 7777; + + Gui g = new Gui(aud,svrflg,volflg,guiflg,port); + g.setSize(500, 500); + g.setLocation(32, 32); + g.setVisible(true); + g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + Server server = new Server(g, port,aud,svrflg,volflg,guiflg); + } + +} diff --git a/CS2452/Skype/src/Skype/Server.java b/CS2452/Skype/src/Skype/Server.java new file mode 100644 index 0000000..e8b8a0b --- /dev/null +++ b/CS2452/Skype/src/Skype/Server.java @@ -0,0 +1,88 @@ +package Skype; + +import java.io.IOException; +import java.net.*; +import java.util.concurrent.Semaphore; + +public class Server extends Thread +{ + ServerSocket ss; + Socket s; + int port; + Gui g; + audio aud; + Semaphore svrflg; + Semaphore audflg; + Semaphore guiflg; + + public Server(Gui g, int port, audio aud, Semaphore svrflg, Semaphore audflg, Semaphore guiflg) + { + this.guiflg = guiflg; + this.audflg = audflg; + this.svrflg = svrflg; + this.aud = aud; + this.port = port; + this.g = g; + try + { + this.ss = new ServerSocket(port); + } catch (IOException e) + { + e.printStackTrace(); + } + + this.start(); + + } + + public void run() + { + while (true) + { + //System.out.printf("Server is waiting\n"); + try + { + svrflg.acquire(); + //System.out.println(ss); + s = ss.accept(); + } catch (IOException e) + { + e.printStackTrace(); + } catch (InterruptedException e) + { + e.printStackTrace(); + } + + try + { + audflg.acquire(); + } catch (InterruptedException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + aud.setOpen(true); + audflg.release(); + //System.out.println("ServerSpeaker"); + speaker t1 = new speaker(s, audio.getFormat(), aud, audflg,g); + mic t2 = new mic(s, audio.getFormat(),g,guiflg); + try + { + guiflg.acquire(); + g.call.setText("Hang up"); + g.sec = 0; + g.timer.start(); + g.incall = true; + guiflg.release(); + } catch (InterruptedException e) + { + e.printStackTrace(); + } + + t1.start(); + t2.start(); + + } + } + +}
\ No newline at end of file diff --git a/CS2452/Skype/src/Skype/audio.java b/CS2452/Skype/src/Skype/audio.java new file mode 100644 index 0000000..d9181fd --- /dev/null +++ b/CS2452/Skype/src/Skype/audio.java @@ -0,0 +1,36 @@ +package Skype; + +import java.net.Socket; + +import javax.sound.sampled.AudioFormat; + +public class audio +{ + double vol; + boolean open; + + audio(int vol) + { + this.vol = vol / 100.00; + } + + public static AudioFormat getFormat() + { + float sampleRate = 8000; + int sampleSizeInBits = 8; + int channels = 1; + boolean signed = true; + boolean bigEndian = true; + return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); + } + + public void setVolume(int volume) + { + vol = volume / 100.00; + } + + public void setOpen(boolean a){ + open = a; + } + +}
\ No newline at end of file diff --git a/CS2452/Skype/src/Skype/mic.java b/CS2452/Skype/src/Skype/mic.java new file mode 100644 index 0000000..01cadfd --- /dev/null +++ b/CS2452/Skype/src/Skype/mic.java @@ -0,0 +1,77 @@ +package Skype; + +import java.util.*; +import java.util.concurrent.Semaphore; + +import javax.sound.sampled.*; + +import java.io.*; +import java.net.*; + +class mic extends Thread +{ + Socket s; + AudioFormat format; + Gui g; + Semaphore guiflg; + + public mic(Socket socket, AudioFormat format, Gui g, Semaphore guiflg) + { + s = socket; + this.format = format; + this.g = g; + this.guiflg = guiflg; + } + + public void run() + { + BufferedOutputStream bout = null; + try + { + DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); + final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info); + + line.open(format, 1024); + line.start(); + int bufferSize = 32; + byte buffer[] = new byte[bufferSize]; + try + { + bout = new BufferedOutputStream(s.getOutputStream()); + + //System.out.printf("mic started\n"); + while (true) + { + int count = line.read(buffer, 0, buffer.length); + + if (count > 0) bout.write(buffer, 0, count); + + bout.flush(); + } + }catch (SocketException se) + { + try + { + guiflg.acquire(); + } catch (InterruptedException e) + { + e.printStackTrace(); + } + + g.call.setText("Call"); + g.incall = false; + guiflg.release(); + + + } + catch (Exception e) + { + e.printStackTrace(); + } + } catch (LineUnavailableException e1) + { + e1.printStackTrace(); + } + } + +} diff --git a/CS2452/Skype/src/Skype/numberFilter.java b/CS2452/Skype/src/Skype/numberFilter.java new file mode 100644 index 0000000..def1b3e --- /dev/null +++ b/CS2452/Skype/src/Skype/numberFilter.java @@ -0,0 +1,48 @@ +package Skype; + +import java.awt.Toolkit; + +import javax.swing.text.AttributeSet; +import javax.swing.text.BadLocationException; +import javax.swing.text.DocumentFilter; + +class numberFilter extends DocumentFilter +{ + @Override + public void insertString(DocumentFilter.FilterBypass fp, int offset, String string,AttributeSet aset) throws BadLocationException + { + int len = string.length(); + boolean isValidInteger = true; + + for (int i = 0; i < len; i++ ) + { + if (!Character.isDigit(string.charAt(i)) && string.charAt(i) != '.') + { + isValidInteger = false; + break; + } + } + if (isValidInteger) super.insertString(fp, offset, string, aset); + else + Toolkit.getDefaultToolkit().beep(); + } + + public void replace(DocumentFilter.FilterBypass fp, int offset, int length, String string, AttributeSet aset) throws BadLocationException + { + int len = string.length(); + boolean isValidInteger = true; + + for (int i = 0; i < len; i++) + { + if (!Character.isDigit(string.charAt(i)) && string.charAt(i) != '.') + { + isValidInteger = false; + break; + } + } + if (isValidInteger) + super.replace(fp, offset, length, string, aset); + else + Toolkit.getDefaultToolkit().beep(); + } +}
\ No newline at end of file diff --git a/CS2452/Skype/src/Skype/speaker.java b/CS2452/Skype/src/Skype/speaker.java new file mode 100644 index 0000000..596986f --- /dev/null +++ b/CS2452/Skype/src/Skype/speaker.java @@ -0,0 +1,90 @@ +package Skype; + +import java.util.*; +import java.util.concurrent.Semaphore; +import java.io.*; +import java.net.*; +import javax.sound.sampled.*; + +class speaker extends Thread +{ + Socket s; + AudioFormat format; + audio aud; + Semaphore flg; + Gui g; + + public speaker(Socket socket, AudioFormat format, audio aud, Semaphore audflg, Gui g) + { + this.g = g; + this.aud = aud; + this.flg = audflg; + s = socket; + this.format = format; + } + + public void run() + { + try + { + DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); + final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); + line.open(format, 1024); + line.start(); + + final AudioInputStream ais = new AudioInputStream(s.getInputStream(), format, 2000000); + int count; + int bufferSize = (int) format.getSampleRate() * format.getFrameSize(); + byte buffer[] = new byte[bufferSize]; + + //System.out.printf("speakers started\n"); + while (((count = ais.read(buffer, 0, buffer.length)) != -1) && aud.open) + { + flg.release(); + if (count > 0) + { + if (flg.tryAcquire()) + { + + for (int i = 0; i < buffer.length; i++ ) + { + buffer[i] *= aud.vol; + } + flg.release(); + } + + line.write(buffer, 0, count); + } + + } + line.drain(); + line.close(); + } catch (IOException e) + { + System.err.println("I/O problems: " + e); + + } catch (LineUnavailableException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + try + { + flg.acquire(); + } catch (InterruptedException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + //System.out.println("out of while"); + try + { + s.close(); + //System.out.println("S closed"); + } catch (IOException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/CS2452/assign2/src/P1.java b/CS2452/assign2/src/P1.java new file mode 100644 index 0000000..3580cf9 --- /dev/null +++ b/CS2452/assign2/src/P1.java @@ -0,0 +1,39 @@ +import java.io.*; + +class P1 +{ + + public static void main(String[] args) + { + String str = null; + BufferedReader br; + br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = null; + + try + { + File file = new File(args[0]); + + if (! file.exists()) + { + file.createNewFile(); + } + + bw = new BufferedWriter(new FileWriter(file)); + while(true) + { + str = br.readLine(); + bw.write(str); + bw.newLine(); + bw.flush(); + } + } catch(Exception e) + { + System.out.printf("Unable to write to file %s\n", args[0]); + } + + + + + } +} diff --git a/CS2452/assign5/src/Client.java b/CS2452/assign5/src/Client.java new file mode 100755 index 0000000..9d7cb19 --- /dev/null +++ b/CS2452/assign5/src/Client.java @@ -0,0 +1,32 @@ +import java.net.*; +import java.io.*; + +class Client +{ + public static void main(String[] args) + { + try + { + byte b = 0; + int cnt = 0; + Socket s = new Socket(args[0], Integer.parseInt(args[1])); + DataInputStream di = new DataInputStream(s.getInputStream()); + while (true) + { + b = di.readByte(); + System.out.printf("%d - %d\t%x\t%c\n", ++cnt, b, b, b); + if ((cnt % (1024 * 1024)) == 0) + { + System.out.println("------MILLION------\n\n"); + cnt = 0; + Thread.sleep(1000); + di.close(); + return; + } + } + } catch (Exception e) + { + e.printStackTrace(); + } + } +}
\ No newline at end of file diff --git a/CS2452/assign5/src/ResourceReader.java b/CS2452/assign5/src/ResourceReader.java new file mode 100755 index 0000000..f9f0f71 --- /dev/null +++ b/CS2452/assign5/src/ResourceReader.java @@ -0,0 +1,89 @@ +import java.io.*; +import java.util.concurrent.Semaphore; + +public class ResourceReader implements Runnable +{ + public static double cor = 0; + public static Semaphore flg1 = new Semaphore(1); + + Thread t; + byte[] res = null, sentinel = null; + DataOutputStream dout = null; + Semaphore flg = null; + Semaphore flg2 = null; + + ResourceReader(DataOutputStream dot, byte[] r, Semaphore s, Semaphore s2) + { + res = r; + dout = dot; + flg = s; + flg2 = s2; + sentinel = new byte[32768]; + for (int i = 0; i < sentinel.length; i++ ) + sentinel[i] = 65; + t = new Thread(this, "ResourceReader"); + t.start(); + } + + public void run() + { + int i; + byte value = 33; + boolean running = true; + + System.out.println("ResourceReader: starts"); + while (running) + { + try + { + + flg2.acquire(); + flg2.release(); + flg1.acquire(); + if (cor == 0) + { + flg.acquire(); + cor++ ; + flg1.release(); + for (int j = 0; j < 50; j++ ) + dout.write(res, 0, res.length); + dout.write(sentinel, 0, sentinel.length); + flg.release(); + } + else + { + cor++ ; + flg1.release(); + for (int j = 0; j < 50; j++ ) + dout.write(res, 0, res.length); + dout.write(sentinel, 0, sentinel.length); + } + flg1.acquire(); + cor-- ; + flg1.release(); + + System.out.println("ResourceReader: sent a bunch"); + Thread.sleep(100); + running = false; + } catch (Exception e) + { + e.printStackTrace(); + try + { + dout.close(); + } catch (Exception e2) + { + } + return; + } + } + System.out.println("ResourceReader: ends"); + try + { + dout.close(); + } catch (Exception e2) + { + } + + } +} diff --git a/CS2452/assign5/src/ResourceWriter.java b/CS2452/assign5/src/ResourceWriter.java new file mode 100755 index 0000000..2fb0967 --- /dev/null +++ b/CS2452/assign5/src/ResourceWriter.java @@ -0,0 +1,56 @@ +import java.util.concurrent.Semaphore; + +public class ResourceWriter implements Runnable +{ + Thread t; + byte[] res = null; + Semaphore flg = null; + Semaphore flg2 = null; + + ResourceWriter(byte[] r, Semaphore s, Semaphore s2) + { + res = r; + flg = s; + flg2 = s2; + t = new Thread(this, "ResourceWriter"); + t.start(); + } + + public void run() + { + int i; + byte value = 33; + boolean running = true; + + System.out.println("ResourceWriter: starts"); + while (running) + { + System.out.println("RW in while"); + try + { + flg2.acquire(); + flg.acquire(); + + for (i = 0; i < res.length; i++ ) + res[i] = value; + flg.release(); + flg2.release(); + } catch (Exception e0) + { + e0.printStackTrace(); + } + + value++ ; + if (value == 100) value = 33; + System.out.println("ResourceWriter: updated, waiting 15, 14, ..."); + try + { + Thread.sleep(15000); + } catch (Exception e) + { + } + } + System.out.println("ResourceWriter: ends"); + + } +} diff --git a/CS2452/assign5/src/Test.java b/CS2452/assign5/src/Test.java new file mode 100755 index 0000000..ed6234c --- /dev/null +++ b/CS2452/assign5/src/Test.java @@ -0,0 +1,23 @@ +class Test +{ + public static void main(String[] args) + { + try + { + if (args.length != 1) + { + System.out.println("usage: java Test listeningPort"); + System.exit(1); + } + System.out.println("Test progam starts"); + + WRServer server = new WRServer(Integer.parseInt(args[0])); + server.t.join(); + + System.out.println("Test progam ends"); + } catch (Exception e) + { + e.printStackTrace(); + } + } +}
\ No newline at end of file diff --git a/CS2452/assign5/src/WRServer.java b/CS2452/assign5/src/WRServer.java new file mode 100755 index 0000000..eadddc3 --- /dev/null +++ b/CS2452/assign5/src/WRServer.java @@ -0,0 +1,59 @@ +import java.io.*; +import java.net.*; +import java.util.concurrent.Semaphore; + +class WRServer implements Runnable +{ + int port; + Thread t; + byte[] theSharedResource = null; + Semaphore flg = null; + Semaphore flg2 = null; + + WRServer(int p) + { + port = p; + theSharedResource = new byte[1024 * 1024]; + flg = new Semaphore(1); + flg2 = new Semaphore(2); + t = new Thread(this, "WRServer"); + t.start(); + } + + public void run() + { + ServerSocket ss = null; + Socket s = null; + DataOutputStream dout = null; + boolean running = true; + System.out.println("WRServer is alive"); + + ResourceWriter rw = new ResourceWriter(theSharedResource, flg, flg2); + + try + { + ss = new ServerSocket(port); + } catch (Exception e0) + { + e0.printStackTrace(); + } + + while (running) + { + try + { + System.out.println("WRServer: waiting for incoming connection"); + s = ss.accept(); + System.out.println("WRServer: received incoming connection"); + + dout = new DataOutputStream(s.getOutputStream()); + ResourceReader rr = new ResourceReader(dout, theSharedResource, flg, flg2); + } catch (Exception e) + { + e.printStackTrace(); + } + } + + System.out.println("WRServer is finished"); + } +} diff --git a/CS2452/chat/ChatClient.java b/CS2452/chat/ChatClient.java new file mode 100644 index 0000000..816d097 --- /dev/null +++ b/CS2452/chat/ChatClient.java @@ -0,0 +1,47 @@ +import java.util.*; +import java.io.*; +import java.net.*; + +class ChatClient +{ + public static void main(String[] args) + { + Socket s; + BufferedReader br, tr; + BufferedWriter bw; + InetAddress address = null; + int port = 0; + + tr = new BufferedReader(new InputStreamReader(System.in)); + + try{ + address = InetAddress.getByName(args[0]); + port = Integer.parseInt(args[1]); + }catch (UnknownHostException ex) + { + ex.printStackTrace(); + } + + try + { + System.out.printf("Connecting...\n"); + s = new Socket(address, port); + + br = new BufferedReader(new InputStreamReader(s.getInputStream())); + + bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); + System.out.printf("Client>"); + + while(true) + { + bw.write(tr.readLine()); + bw.newLine(); + bw.flush(); + System.out.printf("%s: %s\n%s>", "Server", br.readLine(), "Client"); + } + } catch (Exception e) + { + e.printStackTrace(); + } + } +} diff --git a/CS2452/chat/ChatServer.java b/CS2452/chat/ChatServer.java new file mode 100644 index 0000000..3259611 --- /dev/null +++ b/CS2452/chat/ChatServer.java @@ -0,0 +1,44 @@ +import java.io.*; +import java.util.*; +import java.net.*; + +class ChatServer +{ + public static void main(String[] args) + { + ServerSocket ss; + Socket s; + BufferedReader br, tr; + BufferedWriter bw; + + tr = new BufferedReader(new InputStreamReader(System.in)); + + try + { + ss = new ServerSocket(2452); + + s = ss.accept(); + System.out.printf("Client connected...\n"); + + + br = new BufferedReader(new InputStreamReader(s.getInputStream())); + bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); + + //String[] address = new String[2]; + //address[0] = InetAddress.toString(s.getAddress()); + //address[1] = InetAddress.toString(s.getLocalAddress()); + + while(true) + { + System.out.printf("%s: %s\n%s>", "Client", br.readLine(), "Server"); + bw.write(tr.readLine()); + bw.newLine(); + bw.flush(); + } + + }catch (Exception e) + { + e.printStackTrace(); + } + } +} diff --git a/CS2452/chatThreaded/ChatClient.java b/CS2452/chatThreaded/ChatClient.java new file mode 100644 index 0000000..1822240 --- /dev/null +++ b/CS2452/chatThreaded/ChatClient.java @@ -0,0 +1,40 @@ +import java.util.*; +import java.io.*; +import java.net.*; + + + +class ChatClient +{ + public static void main(String[] args) + { + Socket s; + InetAddress address = null; + int port = 0; + + + + try + { + address = InetAddress.getByName(args[0]); + port = Integer.parseInt(args[1]); + }catch (UnknownHostException ex){ + ex.printStackTrace(); + } + + try + { + s = new Socket(address, port); + + T1 screenWriter = new T1(s); + T2 socketWriter = new T2(s); + screenWriter.start(); + socketWriter.start(); + + } + catch(Exception e) + { + e.printStackTrace(); + } + } +} diff --git a/CS2452/chatThreaded/ChatServer.java b/CS2452/chatThreaded/ChatServer.java new file mode 100644 index 0000000..67608d1 --- /dev/null +++ b/CS2452/chatThreaded/ChatServer.java @@ -0,0 +1,27 @@ +import java.util.*; +import java.io.*; +import java.net.*; + +class ChatServer +{ + public static void main(String[] args) + { + ServerSocket ss; + Socket s; + + try + { + ss = new ServerSocket(2452); + s = ss.accept(); + + T1 screenWriter = new T1(s); + T2 socketWriter = new T2(s); + screenWriter.start(); + socketWriter.start(); + } + catch(Exception e) + { + e.printStackTrace(); + } + } +} diff --git a/CS2452/chatThreaded/T1.java b/CS2452/chatThreaded/T1.java new file mode 100644 index 0000000..6fd8e06 --- /dev/null +++ b/CS2452/chatThreaded/T1.java @@ -0,0 +1,33 @@ +import java.util.*; +import java.io.*; +import java.net.*; + + +class T1 extends Thread +{ + Socket s; + + public T1(Socket socket) + { + s = socket; + } + public void run() + { + BufferedReader tr = new BufferedReader(new InputStreamReader(System.in)); + + try + { + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); + + while(true) + { + bw.write(tr.readLine()); + bw.newLine(); + bw.flush(); + } + }catch(Exception e){ + e.printStackTrace(); + } + } +} + diff --git a/CS2452/chatThreaded/T2.java b/CS2452/chatThreaded/T2.java new file mode 100644 index 0000000..470c917 --- /dev/null +++ b/CS2452/chatThreaded/T2.java @@ -0,0 +1,32 @@ + +import java.util.*; +import java.io.*; +import java.net.*; + + +class T2 extends Thread +{ + Socket s; + public T2(Socket socket) + { + s = socket; + } + public void run() + { + + try + { + + BufferedReader bw = new BufferedReader( new InputStreamReader(s.getInputStream())); + String str = "Connected"; + do + { + System.out.printf("%s\n", str); + str = bw.readLine(); + }while (str != null); + }catch(Exception e){ + e.printStackTrace(); + } + } +} + diff --git a/CS2452/sumofdigits.java b/CS2452/sumofdigits.java new file mode 100644 index 0000000..24d4b69 --- /dev/null +++ b/CS2452/sumofdigits.java @@ -0,0 +1,18 @@ + +public class sumofdigits +{ + public static void main(String[] args) + { + System.out.print(sum(23657)); + } + + public static int sum(int num) + { + if(num == 0) + { + return 0; + } + return (num % 10) + sum(num/10); + } + +} |