I have managed to suss out Sockets today and i have got a gui going with netbeans that sends text on button press to a serversocket on another computer. I receive the messages when i open serversocket on simple notepad/cmd. Now i am trying to design a gui for my serversocket. my code that works....
Expand|Select|Wrap|Line Numbers
- import java.net.*;
- import java.io.*;
- public class ConnectTest
- {
- public static void main(String[] args)
- {
- try
- {
- ServerSocket server;
- Socket socket = null;
- BufferedReader inStream = null;
- server = new ServerSocket(4242);
- socket = server.accept();
- inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String aString = inStream.readLine();
- while(aString != "exit")
- {
- System.out.println(aString);
- aString = inStream.readLine();
- }
- socket.close();
- }
- catch(Exception anException)
- {
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- /*
- * Main.java
- *
- * Created on 28 October 2008, 13:19
- *
- * To change this template, choose Tools | Options and locate the template under
- * the Source Creation and Management node. Right-click the template and choose
- * Open. You can then make changes to the template in the Source Editor.
- */
- package clientside;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Main implements ActionListener
- {
- public static JFrame aFrame;
- public static JButton aButton;
- public static JButton aButton2;
- public static JTextField inputArea;
- public static JLabel inputArea2;
- static ServerSocket server;
- static Socket socket;
- static PrintStream outStream;
- static BufferedReader inStream;
- public Main()
- {
- aFrame = new JFrame();
- aFrame.setSize(500, 500);
- aFrame.setTitle("Input your message");
- aFrame.setLayout(new FlowLayout());
- aFrame.setVisible(true);
- aButton = new JButton();
- aButton.setText("Press to send message");
- aButton.setSize(200, 25);
- aButton.addActionListener(this);
- aFrame.add(aButton);
- aButton2 = new JButton();
- aButton2.setText("Press to clear message");
- aButton2.setSize(200, 25);
- aFrame.add(aButton2);
- aButton2.setVisible(true);
- inputArea = new JTextField(50);
- inputArea.setVisible(true);
- aFrame.add(inputArea);
- inputArea2 = new JLabel("hello");
- inputArea2.setVisible(true);
- aFrame.add(inputArea2);
- }
- public static void main(String[] args)
- {
- try
- {
- Main aMain = new Main();
- server = new ServerSocket(4244);
- //socket = new Socket("192.168.0.6", 4242);
- socket = server.accept();
- inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- //outStream = new PrintStream(socket.getOutputStream());
- String aString = inStream.readLine();
- while(aString.equals("exit"))
- {
- Main.inputArea2.setText(aString);
- aString = Main.inStream.readLine();
- }
- }
- catch (Exception anException)
- {
- System.out.println("Error: " + anException);
- }
- }
- public void actionPerformed(ActionEvent e)
- {
- String aString = Main.inputArea.getText();
- if(aString.equals("exit"))
- {
- //socket.close();
- System.exit(1);
- }
- else
- {
- Main.outStream.println(aString);
- aString = Main.inputArea.getText();
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- server = new ServerSocket(4244);
- socket = new Socket("192.168.0.6", 4242);
- socket = server.accept();
- inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- outStream = new PrintStream(socket.getOutputStream());
- String aString = inStream.readLine();