Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Plain Text by Денис ( 14 years ago )
package kontrol;
import java.awt.*;
import java.util.Scanner;
import java.net.ServerSocket.*;
import java.net.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.*;
class UserFrame extends JFrame
{
JTextField in1,in2;
JTextArea Forin1,out1,Forin2,Forout;
public UserFrame() throws HeadlessException {
super("Окно");
setLayout(null);
setSize(500,600);
setTitle("Приложение");
setResizable(false);
//текстовое окошко
Forin1=new JTextArea();
Forin2=new JTextArea();
Forout=new JTextArea();
Forin1.setBounds(5, 10, 60, 15);
Forin2.setBounds(5, 40, 60, 15);
Forout.setBounds(5, 80, 60 ,15);
Forin1.setEnabled(false);
Forin2.setEnabled(false);
Forout.setEnabled(false);
Forin1.setText("Input file ");
Forin2.setText("Output file ");
Forout.setText("action:");
in1=new JTextField();
in2=new JTextField();
in1.setText("input.txt");
in2.setText("outputnyi.txt");
out1=new JTextArea();
in1.setBounds(70, 10, 120, 20);
in2.setBounds(70, 40, 120, 20);
out1.setBounds(10,100, 380, 400);
this.add(in1);
this.add(in2);
this.add(out1);
this.add(Forin1);
this.add(Forout);
this.add(Forin2);
//ryjgrb
JButton cl=new JButton("Close");
JButton st=new JButton("Start");
cl.setBounds(10, 530, 150, 20);
st.setBounds(10, 500, 150, 20);
add(cl);
add(st);
st.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String INFA =in1.getText();
String OUFA =in2.getText();
int serverPort = 2011; // здесь обязательно нужно указать порт к которому привязывается сервер.
String address = "127.0.0.1"; // это IP-адрес компьютера, где исполняется наша серверная программа.
// Здесь указан адрес того самого компьютера где будет исполняться и клиент.
try {
FileWriter kok=new FileWriter(OUFA);
InetAddress ipAddress = InetAddress.getByName(address); // создаем объект который отображает вышеописанный IP-адрес.
out1.append("попытка подключения");
kok.write("попытка подключения \n");
out1.append("\n");
Socket socket = new Socket(ipAddress, serverPort); // создаем сокет используя IP-адрес и порт сервера.
out1.append("подключились");
kok.write(" подключились \n");
out1.append("\n");
// Берем входной и выходной потоки сокета, теперь можем получать и отсылать данные клиентом.
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
// Конвертируем потоки в другой тип, чтоб легче обрабатывать текстовые сообщения.
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
// Создаем поток для чтения с клавиатуры.
FileInputStream is=new FileInputStream(INFA);
Scanner tekct = new Scanner(is);
if(!tekct.hasNext()){out1.setText("проверь файл");return;}
String line = null;
System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
System.out.println();
StringBuffer mem=new StringBuffer(1000);
while (tekct.hasNext()) {
line = tekct.nextLine();
System.out.println("Sending this line to the server...");
out.writeUTF(line); // отсылаем введенную строку текста серверу.
out1.append("отправили строку");
kok.write("отправили строку \n");
out1.append("\n");
out.flush(); // заставляем поток закончить передачу данных.
line = in.readUTF();//принята
kok.write(line+"\n");
out1.append(line);
out1.append("\n");
line = in.readUTF();//обработана
kok.write(line+"\n");
out1.append(line);
out1.append("\n");
line = in.readUTF(); // сама строка
mem.append(line);
mem.append('\n');
out1.append("нам вернули строчку");
kok.write("нам вернули строчку \n");
out1.append("\n");
out1.append(line);
out1.append("\n");
kok.write(line);
kok.write("\n");
System.out.println("The server was very polite. It sent me this : " + line);
System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
System.out.println();
}
kok.write("пока всё \n");
kok.flush();
kok.close();
} catch (Exception x) {
x.printStackTrace();
}
}
});
cl.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
System.exit(0);
}
});
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// face.addKeyListener(new java.awt.event.KeyAdapter() {
}
}
public class Kontrol
{
public static void main(String[] args) throws UnknownHostException, IOException
{
UserFrame UF= new UserFrame();
}
}//127001
public UserFrame() throws HeadlessException {
Forin1=new JTextArea();
Forin2=new JTextArea();
Forout=new JTextArea();
Forin1.setBounds(5, 10, 60, 15);
Forin2.setBounds(5, 40, 60, 15);
Forout.setBounds(5, 80, 60 ,15);
Forin1.setEnabled(false);
Forin2.setEnabled(false);
Forout.setEnabled(false);
Forin1.setText("Input file ");
Forin2.setText("Output file ");
Forout.setText("action:");
in1=new JTextField();
in2=new JTextField();
in1.setText("input.txt");
in2.setText("outputnyi.txt");
out1=new JTextArea();
in1.setBounds(70, 10, 120, 20);
in2.setBounds(70, 40, 120, 20);
out1.setBounds(10,100, 380, 400);
this.add(in1);
this.add(in2);
this.add(out1);
this.add(Forin1);
this.add(Forout);
this.add(Forin2);
//ryjgrb
JButton cl=new JButton("Close");
JButton st=new JButton("Start");
cl.setBounds(10, 530, 150, 20);
st.setBounds(10, 500, 150, 20);
add(cl);
add(st);
Revise this Paste