---------------------- android培训、java培训、期待与您交流! ----------------------
//上传图片
//可登录三次的服务
//以浏览器为客户端提供连接
//URL对象
import java.io.*;
import java.net.*;
class URLDemo
{
public static void main(String[] args) throws Exception
{
URL url = new URL("http://127.0.0.1:12000/?name=haha&age=30");
// URLConnection con = url.openConnection();
// System.out.println(con);
// InputStream in = con.getInputStream();
// byte[] buffer = new byte[1024];
// int length = in.read(buffer);
// System.out.println(new String(buffer,0,length));
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println(url.getQuery());
}
}
class TCPServer2
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(12000);
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostAddress());
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("欢迎");
s.close();
ss.close();
}
}
class TCPClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("127.0.0.1",12000);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader brin = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
for (int x = 0;x < 3 ;x++ )
{
String line = br.readLine();
if(line.trim().equals("") || line == null)
{
break;
}
out.println(line);
String linein = brin.readLine();
System.out.println("Info : "+linein);
if (linein.contains("欢迎"))
{
break;
}
}
br.close();
brin.close();
s.close();
}
}
class TCPLogin implements Runnable
{
private Socket s;
TCPLogin(Socket s)
{
this.s = s;
}
public void run()
{
System.out.println(s.getInetAddress().getHostAddress()+".connected");
try
{
for (int x = 0;x < 3;x++)
{
BufferedReader brin = new BufferedReader(new InputStreamReader(s.getInputStream()));
String data = brin.readLine();
if (data == null)
{
break;
}
BufferedReader br = new BufferedReader(new FileReader("UserInfo.ini"));
String line = null;
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
boolean flag = false;
while ((line = br.readLine()) != null)
{
if (line.equals(data))
{
flag = true;
break;
}
}
if (flag)
{
System.out.println(data+",已登录");
out.println(data+",欢迎");
break;
}
else
{
System.out.println(data+",尝试登录");
out.println(data+",用户不存在");
}
}
s.close();
}
catch (Exception e)
{
throw new RuntimeException("用户校验失败");
}
}
}
class TCPServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(12000);
while (true)
{
Socket s = ss.accept();
new Thread(new TCPLogin(s)).start();
}
}
}
class PicClient
{
public static void main(String[] args) throws Exception
{
FileInputStream fis = new FileInputStream("f:\\java\\test\\b.jpg");
byte[] buffer = new byte[1024];
int length = 0;
Socket s = new Socket("127.0.0.1",12000);
OutputStream out = s.getOutputStream();
while ((length = fis.read(buffer)) != -1)
{
out.write(buffer,0,length);
out.flush();
}
s.shutdownOutput();
InputStream in = s.getInputStream();
length = in.read(buffer);
System.out.println(new String(buffer,0,length));
s.close();
fis.close();
}
}
class PicRec implements Runnable
{
private ServerSocket ss;
PicRec(ServerSocket ss)
{
this.ss = ss;
}
public void run ()
{
try
{
int count = 1;
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + "...connected");
File file = new File(ip + count + ".jpg");
while (!(file.exists() && file.isFile()))
{
file = new File(ip +(count++)+ ".jpg");
break;
}
FileOutputStream fos = new FileOutputStream(file);
InputStream in = s.getInputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) != -1)
{
fos.write(buffer,0,length);
fos.flush();
}
fos.close();
OutputStream out = s.getOutputStream();
out.write("文件上传成功".getBytes());
s.close();
}
catch (Exception e)
{
throw new RuntimeException("文件上传失败");
}
}
}
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(12000);
while (true)
{
new Thread(new PicRec(ss)).start();
}
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------