//为什么没有反应呢???
/**最小的TCP/IP服务器
*依赖于Java提供的ServerSocket和Socket网络连接类.ServerSocket类做了大多数工作.
*/
import java.net.*;
import java.io.*;class SimpleServer
{
public static void main(String[] args)
{
ServerSocket s=null; //注册你的服务器
try{
s=new ServerSocket(5432);
}catch(IOException e){
e.printStackTrace();//打印错误信息
}
while(true){
try{
//服务器等待连接的监听
Socket s1=s.accept();
OutputStream s1out=s1.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s1out));
bw.write("Hello Maozhuxi");
bw.close();//关闭连接,但是没有关闭服务器.
s1.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
========
/**最小的客户端
*TCP/IP应用程序的客户端依赖于Socket类.
*同样,Socket类做了建立连接的大部分工作.
*/
import java.net.*;
import java.io.*;class SimpleClient
{
public static void main(String[] args)
{
try{
//打开一个针对服务器的连接,在端口5432
Socket s1=new Socket("192.168.9.89",5432);
//在此端口获得一个输入流
InputStream is=s1.getInputStream();
DataInputStream dis=new DataInputStream(is); System.out.println(dis.readUTF());
is.close();
dis.close();
}catch(ConnectException e){
System.err.println("不能建立连接.");
}
catch(IOException e){
//
}
}
}
/**最小的TCP/IP服务器
*依赖于Java提供的ServerSocket和Socket网络连接类.ServerSocket类做了大多数工作.
*/
import java.net.*;
import java.io.*;class SimpleServer
{
public static void main(String[] args)
{
ServerSocket s=null; //注册你的服务器
try{
s=new ServerSocket(5432);
}catch(IOException e){
e.printStackTrace();//打印错误信息
}
while(true){
try{
//服务器等待连接的监听
Socket s1=s.accept();
OutputStream s1out=s1.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s1out));
bw.write("Hello Maozhuxi");
bw.close();//关闭连接,但是没有关闭服务器.
s1.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
========
/**最小的客户端
*TCP/IP应用程序的客户端依赖于Socket类.
*同样,Socket类做了建立连接的大部分工作.
*/
import java.net.*;
import java.io.*;class SimpleClient
{
public static void main(String[] args)
{
try{
//打开一个针对服务器的连接,在端口5432
Socket s1=new Socket("192.168.9.89",5432);
//在此端口获得一个输入流
InputStream is=s1.getInputStream();
DataInputStream dis=new DataInputStream(is); System.out.println(dis.readUTF());
is.close();
dis.close();
}catch(ConnectException e){
System.err.println("不能建立连接.");
}
catch(IOException e){
//
}
}
}
import java.net.*;
public class tcpserver
{
public static void main(String[] args) throws IOException
{
ServerSocket svrsoc=null;
Socket soc=null;
DataInputStream in=null;
PrintStream out=null;
InetAddress clientIP=null;
String str=null;
try
{
svrsoc=new ServerSocket(8000);
System.out.println("Server start....");
soc=svrsoc.accept();in=new DataInputStream(soc.getInputStream());
out=new PrintStream(soc.getOutputStream());
clientIP=soc.getInetAddress();
System.out.println("Client's IP address:"+clientIP);
out.println("welcome.....");
str=in.readLine();
while (!str.equals("quit"))
{
System.out.println("Client said:"+str);
str=in.readLine();
}
System.out.println("Client want to leave");
}
catch(Exception e)
{
System.out.println("error:"+e);
}
finally
{
in.close();
out.close();
soc.close();
svrsoc.close();
System.exit(0);
}
}
}//客户端源程序tcpclient.javaimport java.io.*;
import java.net.*;
public class tcpclient
{
public static void main(String[] args) throws IOException
{
Socket soc=null;
DataInputStream in=null;
PrintStream out=null;
DataInputStream sysin=null;
String strin=null;
String strout=null;
try
{
soc=new Socket(args[0],8000);
System.out.println("Connecting to the Server");
in=new DataInputStream(soc.getInputStream());
out=new PrintStream(soc.getOutputStream());
strin=in.readLine();
System.out.println("Server said:"+strin);
sysin=new DataInputStream(System.in);
strout=sysin.readLine();
while (!strout.equals("quit"))
{
out.println(strout);
strout=sysin.readLine();
}
out.println(strout);
}
catch(Exception e)
{
System.out.println("error:"+e);
}
finally
{
in.close();
out.close();
soc.close();
sysin.close();
System.exit(0);
}
}
}
try{
s=new ServerSocket(5432);
}catch(IOException e){
e.printStackTrace();//打印错误信息
}
while(true){
try{
//服务器等待连接的监听
Socket s1=s.accept(); OutputStream s1out=s1.getOutputStream();
BufferedWriter bw=new ....
红字的变量是你源程序错了还是你往网上沾的时候错了
是不是抛出NullPointerException了