自己家里两台电脑用路由器上网,使用java socket通讯如何设ip让一台电脑作客户端另一台做服务端? 5
客户端importjava.net.*;importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjav...
客户端
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client
{ public static void main(String args[])
{ new ComputerClient();
}
}
class ComputerClient extends JFrame implements Runnable,ActionListener
{ JButton connection,send;
JTextField inputText;
JTextArea showResult;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
ComputerClient()
{ socket=new Socket();
connection=new JButton("连接服务器");
send=new JButton("发送");
send.setEnabled(false);
inputText=new JTextField(6);
showResult=new JTextArea();
add(connection,BorderLayout.NORTH);
JPanel pSouth=new JPanel();
pSouth.add(new JLabel("输入三角形三边的长度,用逗号或空格分隔:"));
pSouth.add(inputText);
pSouth.add(send);
add(new JScrollPane(showResult),BorderLayout.CENTER);
add(pSouth,BorderLayout.SOUTH);
connection.addActionListener(this);
send.addActionListener(this);
thread=new Thread(this);
setBounds(10,30,460,400);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==connection)
{ try //请求和服务器建立套接字连接:
{ if(socket.isConnected())
{}
else
{ InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=
new InetSocketAddress(address,4331);
socket.connect(socketAddress);
in =new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
send.setEnabled(true);
if(!(thread.isAlive()))
thread=new Thread(this);
thread.start();
}
}
catch (IOException ee)
{ System.out.println(ee);
socket=new Socket();
}
}
if(e.getSource()==send)
{ String s=inputText.getText();
if(s!=null)
{ try { out.writeUTF(s);
}
catch(IOException e1){}
}
}
}
public void run()
{ String s=null;
double result=0;
while(true)
{ try{ s=in.readUTF();
result=in.readDouble();
showResult.append("\n"+s+"\n"+result);
}
catch(IOException e)
{ showResult.setText("与服务器已断开"+e);
socket=new Socket();
break;
}
}
}
}
服务器端
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{ public static void main(String args[])
{ ServerSocket server=null;
Server_thread thread;
Socket you=null;
while(true)
{ try{ server=new ServerSocket(4331);
}
catch(IOException e1)
{ System.out.println("正在监听"); //ServerSocket对象不能重复创建
}
try{ System.out.println(" 等待客户呼叫");
you=server.accept();
System.out.println("客户的地址:"+you.getInetAddress());
}
catch (IOException e)
{ System.out.println("正在等待客户");
}
if(you!=null)
{ new Server_thread(you).start(); //为每个客户启动一个专门的线程
} 展开
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client
{ public static void main(String args[])
{ new ComputerClient();
}
}
class ComputerClient extends JFrame implements Runnable,ActionListener
{ JButton connection,send;
JTextField inputText;
JTextArea showResult;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
ComputerClient()
{ socket=new Socket();
connection=new JButton("连接服务器");
send=new JButton("发送");
send.setEnabled(false);
inputText=new JTextField(6);
showResult=new JTextArea();
add(connection,BorderLayout.NORTH);
JPanel pSouth=new JPanel();
pSouth.add(new JLabel("输入三角形三边的长度,用逗号或空格分隔:"));
pSouth.add(inputText);
pSouth.add(send);
add(new JScrollPane(showResult),BorderLayout.CENTER);
add(pSouth,BorderLayout.SOUTH);
connection.addActionListener(this);
send.addActionListener(this);
thread=new Thread(this);
setBounds(10,30,460,400);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==connection)
{ try //请求和服务器建立套接字连接:
{ if(socket.isConnected())
{}
else
{ InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=
new InetSocketAddress(address,4331);
socket.connect(socketAddress);
in =new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
send.setEnabled(true);
if(!(thread.isAlive()))
thread=new Thread(this);
thread.start();
}
}
catch (IOException ee)
{ System.out.println(ee);
socket=new Socket();
}
}
if(e.getSource()==send)
{ String s=inputText.getText();
if(s!=null)
{ try { out.writeUTF(s);
}
catch(IOException e1){}
}
}
}
public void run()
{ String s=null;
double result=0;
while(true)
{ try{ s=in.readUTF();
result=in.readDouble();
showResult.append("\n"+s+"\n"+result);
}
catch(IOException e)
{ showResult.setText("与服务器已断开"+e);
socket=new Socket();
break;
}
}
}
}
服务器端
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{ public static void main(String args[])
{ ServerSocket server=null;
Server_thread thread;
Socket you=null;
while(true)
{ try{ server=new ServerSocket(4331);
}
catch(IOException e1)
{ System.out.println("正在监听"); //ServerSocket对象不能重复创建
}
try{ System.out.println(" 等待客户呼叫");
you=server.accept();
System.out.println("客户的地址:"+you.getInetAddress());
}
catch (IOException e)
{ System.out.println("正在等待客户");
}
if(you!=null)
{ new Server_thread(you).start(); //为每个客户启动一个专门的线程
} 展开
4个回答
展开全部
把路由器拔了 用ipconfig 看一下两个电脑的IP 在程序里设置一下IP 选择同一个port即端口
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在客户端 设好服务端的ip 先运行服务端,在运行客户端就可以了 还有 你的两台电脑的ip是由路由器自动分配的静态ip(私有ip),即“假”ip,这是“非法”ip。你可以在dos窗口运行ipconfig找到你的静态ip,在局域网内Socket通信可以直接联通的 建议采纳!!!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询