socketclient.java
来自「有关java的源程序,为讲授java程序设计课程使用」· Java 代码 · 共 51 行
JAVA
51 行
import java.io.*;
import java.net.*;
public class SocketClient
{ public static void main(String args[])
{
String HostName="192.168.0.6";
Socket s=null;
int port=1000;
DataInputStream sin=null;
PrintStream sout=null;
DataInputStream dis=new DataInputStream(System.in);
try
{ s=new Socket(HostName,port);
sin=new DataInputStream(s.getInputStream());
sout=new PrintStream(s.getOutputStream());
}
catch(UnknownHostException e)
{ System.err.println("Don't know about host"+args[0]);
}
catch(IOException e)
{ System.err.println("Canot get I/O for the Connect to"+HostName);
}
System.out.println("Connected to"+s.getInetAddress()+":"+s.getPort());
String text;
try
{
while(true)
{ System.out.print(":");
System.out.flush();
if((text =dis.readLine())!=null)
{ sout.println(text);
if((text =sin.readLine())!=null)
System.out.println(text);
else
{ System.out.println("Connection closed by server");
break;
}
}
else
break;
}
sin.close();
sout.close();
s.close();
}
catch(IOException e)
{ System.err.println("I/O failed on the connection to"+HostName);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?