Advertisement
Guest User

Untitled

a guest
Sep 14th, 2012
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. SERVER:
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6.  
  7.  
  8. public class Server {
  9.  
  10.  
  11.  
  12. public static void main(String[] args) {
  13.  
  14.  
  15.  
  16. try {
  17. while (true) {
  18. ServerSocket sock = new ServerSocket(6013);
  19. // now listen for connections
  20.  
  21. Socket server = sock.accept();
  22. InputStream in = server.getInputStream();
  23. BufferedReader bin = new
  24. BufferedReader(new InputStreamReader(in));
  25.  
  26. String line;
  27. while ( (line = bin.readLine()) != null)
  28. System.out.println("Server recieved: " + line);
  29. //PrintWriter pout = new
  30. //PrintWriter(client.getOutputStream(), true);
  31. // write the Date to the socket
  32. //pin.println(new java.util.Date().toString());
  33. //pout.
  34. //System.out.println("Server recieved: " + client.getOutputStream().);
  35. // close the socket and resume
  36. // listening for connections
  37. sock.close();
  38. }
  39. }
  40. catch (IOException ioe) {
  41. System.err.println(ioe);
  42. }
  43. }
  44. }
  45.  
  46. ---------------------------------------------------------------------------------------------
  47. CLIENT:
  48.  
  49. import java.util.*;
  50. import java.awt.*;
  51. import java.awt.event.*;
  52. import javax.swing.*;
  53. import java.net.*;
  54. import java.io.*;
  55.  
  56. public class GUI_Client {
  57.  
  58. //Variable Definitions for the class
  59. public static JFrame frame;
  60. public static GUI_Client Gui;
  61. public static String message;
  62.  
  63. //Main method will be run once the program starts. This main calls makeFrame.
  64. public static void main(String[] args) {
  65. //GUI_Client Gui = new GUI_Client();
  66. makeFrame();
  67.  
  68. }
  69.  
  70. //makeFrame gets called by the main. This method builds te GUI.
  71. public static void makeFrame() {
  72. frame = new JFrame("Client");
  73. Container contentPane = frame.getContentPane();
  74. GridLayout gridLayout = new GridLayout(1,2);
  75. contentPane.setLayout(gridLayout);
  76.  
  77. final JTextField JTmessage = new JTextField("Message");
  78. JTmessage.setEditable(true);
  79. contentPane.add(JTmessage);
  80.  
  81. JButton sendMessage = new JButton("Send Message");
  82. contentPane.add(sendMessage);
  83.  
  84. sendMessage.addActionListener(new ActionListener(){
  85. public void actionPerformed(ActionEvent e) {
  86. String message = JTmessage.getText();
  87. try {
  88. //make connection to server socket
  89. Socket client = new Socket("127.0.0.1",6013);
  90.  
  91. PrintWriter pout = new
  92. PrintWriter(client.getOutputStream(), true);
  93.  
  94. // read the date from the socket
  95. String line = message;
  96. pout.println(line);
  97.  
  98. // close the socket connection
  99. client.close();
  100. }
  101. catch (IOException ioe) {
  102. System.err.println(ioe);
  103. }
  104. }
  105. });
  106. frame.pack();
  107. frame.setVisible(true);
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement