package client;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class Server {
@SuppressWarnings("resource")
public static void main(String[] args) {
ServerSocket server =null;
ServerThread thread;
Socket you= null;
while (true)
{
try{server = new ServerSocket(4331);}
catch (IOException e1){System.out.println("正在监听");}
try{you=server.accept();
System.out.println("客户的地址:"+you.getInetAddress());}
catch(IOException e){System.out.println("正在等待客户");}
if(you!=null){
new ServerThread(you).start();
}
}
}
}
class ServerThread extends Thread
{
Socket socket;
ObjectInputStream in=null;
ObjectOutputStream out =null;
JFrame window;
JTextArea text;
ServerThread(Socket t)
{
socket =t;
try
{
out=new ObjectOutputStream(socket.getOutputStream());
in =new ObjectInputStream(socket.getInputStream());
}
catch (IOException e){}
window =new JFrame();
text =new JTextArea();
for(int i=1;i<=20;i++)
{
text.append("你好,我是服务器上的文件区组件\n");
}
text.setBackground(Color.yellow);
window.add(text);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run(){
try{ out.writeObject(window);}
catch (IOException e){
System.out.println("客户离开");
}
}
}
附客服代码
package client;
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
public static void main(String agrs[])
{
Scanner scanner= new Scanner (System.in);
Socket mysocket=null;
ObjectInputStream inObject=null;
ObjectOutputStream outObject=null;
Thread thread;
ReadWindow readWindow =null;
try {
mysocket =new Socket();
readWindow =new ReadWindow();
thread =new Thread(readWindow);
System.out.println("输入服务器的IP");
String IP=scanner.nextLine();
System.out.println("输入端口号:");
int port =scanner.nextInt();
if(mysocket.isConnected())
{
}
else
{
InetAddress address =InetAddress.getByName(IP);
InetSocketAddress socketAddress=new InetSocketAddress(address,port);
mysocket.connect(socketAddress);
InputStream in=mysocket.getInputStream();
OutputStream out=mysocket.getOutputStream();
inObject=new ObjectInputStream(in);
outObject=new ObjectOutputStream(out);
readWindow.setObjectInputStream(inObject);
thread.start();
}
}
catch(Exception e)
{
System.out.println("服务器已经断开"+e);
}
}
}
class ReadWindow implements Runnable
{
ObjectInputStream in;
public void setObjectInputStream( ObjectInputStream in)
{
this .in=in;
}
public void run()
{
double result =0;
while (true)
{
try{
javax.swing.JFrame window=(javax.swing.JFrame)in.readObject();
window.setTitle("这是从服务器上读入的窗口");
window.setVisible(true);
window.requestFocusInWindow();
window.setSize(600, 800);
}
catch (Exception e)
{
System.out.println("服务器已经断开"+e);
break;
}
}
}
}
添加回答
举报
0/150
提交
取消