Socket sk = new Socket("210.0.235.14",13);
InputStream inStream = sk.getInputStream();
try
{
Socket sk = new Socket("210.0.235.14",13);
sk.setSoTimeout(3000);
InputStream inStream = sk.getInputStream();
//得到输入流对象
Scanner sc = new Scanner(inStream);
//将数据打印到控制台
while(sc.hasNextLine())
{
String str = sc.nextLine();
System.out.println("Output : " + str);
}
sk.close();
}
catch(SocketTimeoutException e) //超时异常
{
System.out.println("Time Out!");
}
catch(Exception e)
{
e.printStackTrace();
}
new Socket(host,port);
Socket sk = new Socker(); sk.connect(new InetSocketAddress(host,port),2000); //设置超时时间为2秒
package cls;
import java.net.*;
public class ShowIP
{
public static void main(String[] args)
{
try
{
if(args.length > 0)
{
String hostName = args[0]; //主机名
InetAddress[] addr = InetAddress.getAllByName(hostName);
//得到该主机的所有地址
//打印输出至控制台
for(InetAddress address : addr)
{
System.out.println(address.getHostAddress());
}
}
else
{
System.out.println(InetAddress.getLocalHost().getHostAddress());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
ServerSocket sock = new ServerSocker(8000);
package cls;
import java.io.*;
import java.net.*;
import java.util.*;
public class ServerDemo
{
/**
* @param args
*/
public static void main(String[] args)
{
try
{
//ServerSocket servSocket = new ServerSocket(8000);
ServerSocket servSocket = new ServerSocket(8400);
int amount = 0;
while(true)
{
Socket client = servSocket.accept();
++amount;
Date time = new Date();
String prompt = time.toString() + ": 第" + amount + "个用户 " + client.getInetAddress().getHostAddress() + " 已连接\n";
System.out.print(prompt); //在控制台输出信息
ServerDemo.writeLog(prompt); //写入到文件中
//start a new Thread
Thread th = new Thread(new ServThread(client,amount));
th.start();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
//写入日志文件
public static void writeLog(String str)
{
File logFile = new File("server-log.txt");
try
{
FileWriter out = new FileWriter(logFile,true);
out.append(str);
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
/*
* 服务线程类
*/
class ServThread implements Runnable
{
private Socket client;
private int ix;
public ServThread(Socket soc,int ix)
{
client = soc;
this.ix = ix;
}
public void run()
{
try
{
InputStream inStream = client.getInputStream();
OutputStream outStream = client.getOutputStream();
Scanner recv = new Scanner(inStream);
PrintWriter send = new PrintWriter(outStream,true);
send.println("欢迎~随便聊几句吧![输入'bye'关闭联接]");
while(recv.hasNextLine())
{
String str = recv.nextLine();
if(str.equals("bye"))
{
send.println("See you later ~ ^-^");
break;
}
send.println("这是个测试程序,现在还没有什么功能哦");
}
Date time = new Date();
String prompt = time.toString() + ": 第" + ix + "个用户 " + client.getInetAddress().getHostAddress() + " 已断开连接\n";
System.out.print(prompt);
ServerDemo.writeLog(prompt); //写入到文件中
client.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有