//2.[Client.java]
package smssocket;
import java.io.*;
import java.net.*;public class Client {
public static void main(String args[]) {
//args 是main方法的入参(String数组),在这里应该只有一个,是Server 的name地址
try{
if (args.length != 1){
System.out.println("USAGE: java Client servername");
return;
}
String connectto= args[0];//把入参的第0个字符串赋给connectto
Socket connection;if(connectto.equals("localhost")){
connection=new Socket(InetAddress.getLocalHost(),5000);
//建立一个和本机的Socket连接(Server是本机,也就是说Server、Client在一台机器上),端口用5000
//InetAddress类描述IP地址,InetAddress.getLocalHost()得到本地IP地址,返回类型是InetAddress
}
else{
connection=new Socket(InetAddress.getByName(connectto),5000);
//建立一个和指定服务器的Socket连接,端口为5000
//InetAddress.getByName()根据指定名称,找到IP地址
}
DataInputStream input=new DataInputStream(connection.getInputStream());
//得到输入流String info;
info = input.readUTF();
//以Unicode编码从输入流中读取字符串
System.out.println(info);
connection.close();
//关闭Socket连接
}
catch(SecurityException e){
System.out.println("SecurityException when connecting Server!");
}
catch(IOException e){
System.out.println("IOException when connecting Server!");
}
}}
建议你看代码的时候参考java API文档。
package smssocket;
import java.io.*;
import java.net.*;public class Client {
public static void main(String args[]) {
//args 是main方法的入参(String数组),在这里应该只有一个,是Server 的name地址
try{
if (args.length != 1){
System.out.println("USAGE: java Client servername");
return;
}
String connectto= args[0];//把入参的第0个字符串赋给connectto
Socket connection;if(connectto.equals("localhost")){
connection=new Socket(InetAddress.getLocalHost(),5000);
//建立一个和本机的Socket连接(Server是本机,也就是说Server、Client在一台机器上),端口用5000
//InetAddress类描述IP地址,InetAddress.getLocalHost()得到本地IP地址,返回类型是InetAddress
}
else{
connection=new Socket(InetAddress.getByName(connectto),5000);
//建立一个和指定服务器的Socket连接,端口为5000
//InetAddress.getByName()根据指定名称,找到IP地址
}
DataInputStream input=new DataInputStream(connection.getInputStream());
//得到输入流String info;
info = input.readUTF();
//以Unicode编码从输入流中读取字符串
System.out.println(info);
connection.close();
//关闭Socket连接
}
catch(SecurityException e){
System.out.println("SecurityException when connecting Server!");
}
catch(IOException e){
System.out.println("IOException when connecting Server!");
}
}}
建议你看代码的时候参考java API文档。
解决方案 »
- java 最接近点对 栈溢出error
- 在JPANEL上动态的改变背景,并且能有PPT一样的特效
- eclispe编写java程序运行时需要最大化才能看到菜单
- 边界标志算法
- javadoc转换为chm的问题
- Graphics.drawLine方法出现异常NullPointerException
- 求Jsp日期控件
- 读取图片进行像素读取和处理必须在JApplet下完成么,能否直接在JFrame下完成
- socket中字节转换成汉字的问题?急
- 请问哪儿有jdk1.3下载,速度比较快一点的网址?
- 求救怎样通过Java对FTP虚拟目录操作,怎样获取linux上的各分区磁盘剩余空间?
- 网页中如何插入applet?要支持ie6的
args 那个相当于VC的commandline,DOS命令后的参数元,建议学点计算机基础