服务器端
import java.net.*;
import java.io.*;
public class Server
{
public static void main(String [] args)
{
new Server().IntSocket();
}
public void IntSocket()
{
ServerSocket m_Socket=null;
Socket   Socket_Accpet=null;
try
{
m_Socket=new ServerSocket(3000);
System.out.println("开始监听客户端请求....");
Socket_Accpet=m_Socket.accept();
new Accept(Socket_Accpet);
System.out.println("已经连接上一个客户端....");
}
catch(IOException e1){e1.toString();}
catch(Exception e){e.toString();}
}
}
class Accept extends Thread
{
Socket xSocket=null;
BufferedReader in=null;
public Accept(Socket eSocket)
{
try
{
xSocket=eSocket;
System.out.println("创建接收数据流....");
in=new BufferedReader(new InputStreamReader(xSocket.getInputStream()));
this.start();
}
catch(IOException e1){e1.toString();}
catch(Exception e){e.toString();}
}
public void run()
{
String result=null;
System.out.println("开始接收数据....");
while(true)
{
try

result=in.readLine();
if(result!=null)
System.out.println(result);
sleep(100);
}
catch(IOException e1){e1.toString();}
catch(Exception e){e.toString();}
}
}
}
客户端:
import java.net.*;
import java.io.*;
public class ClientSocket
{
public static void main(String [] args)
{
new ClientSocket().IntSocket();
}
public void IntSocket()
{
Socket  Socket_Connection=null;
try
{
System.out.println("创建套接字....");
Socket_Connection=new Socket("127.0.0.1",3000);
System.out.println("连接服务器端....");
new Client_Connection(Socket_Connection);
}
catch(IOException e1){e1.toString();}
catch(Exception e){e.toString();}
}
}
class Client_Connection extends Thread
{
BufferedWriter out=null;
Socket x_Socket=null;
public Client_Connection(Socket m_Socket)
{
x_Socket=m_Socket;
System.out.println("创建发送数据流....");
try
{
out=new BufferedWriter(new OutputStreamWriter(x_Socket.getOutputStream()));
this.start();
}
catch(IOException e1){e1.toString();}
catch(Exception e){e.toString();}
}
public void run()
{
String result="终于成功了............";
System.out.println("开启发送线程....");
while(true)
{
try
{
 out.write(result,0,100);
 out.flush();
 System.out.println(result);
}
catch(IOException e1){e1.toString();}
catch(Exception e){e.toString();}
}
}
}
请帮我看看什么问题?

解决方案 »

  1.   

    //客户端:
    import java.net.*;
    import java.io.*;
    public class ClientSocket
    {
    public static void main(String [] args)
    {
    new ClientSocket().IntSocket();
    }
    public void IntSocket()
    {
    Socket  Socket_Connection=null;
    try
    {
    System.out.println("创建套接字....");
    Socket_Connection=new Socket("127.0.0.1",3000);
    System.out.println("连接服务器端....");
    new Client_Connection(Socket_Connection);
    }
    catch(IOException e1){e1.toString();}
    catch(Exception e){e.toString();}
    }
    }
    class Client_Connection extends Thread
    {
    BufferedWriter out=null;
    Socket x_Socket=null;
    public Client_Connection(Socket m_Socket)
    {
    x_Socket=m_Socket;
    System.out.println("创建发送数据流....");
    try
    {
    out=new BufferedWriter(new OutputStreamWriter(x_Socket.getOutputStream()));
    this.start();
    }
    catch(IOException e1){e1.toString();}
    catch(Exception e){e.toString();}
    }
    public void run()
    {
    String result="终于成功了............";
    System.out.println("开启发送线程....");
    while(true)
    {
    try
    {
     // 更改 
     out.write(result,0,result.length());
     out.newLine();  // 添加
     out.flush();    // 提交缓冲流
     System.out.println(result);
     sleep(1000);
    }
    catch(IOException e1){e1.toString();}
    catch(Exception e){e.toString();}
    }
    }
    }
      

  2.   

    //客户端:
    import java.net.*;
    import java.io.*;
    public class ClientSocket
    {
    public static void main(String [] args)
    {
    new ClientSocket().IntSocket();
    }
    public void IntSocket()
    {
    Socket  Socket_Connection=null;
    try
    {
    System.out.println("创建套接字....");
    Socket_Connection=new Socket("127.0.0.1",3000);
    System.out.println("连接服务器端....");
    new Client_Connection(Socket_Connection);
    }
    catch(IOException e1){e1.toString();}
    catch(Exception e){e.toString();}
    }
    }
    class Client_Connection extends Thread
    {
    //BufferedWriter out=null;
    OutputStream out = null;  //修改
    Socket x_Socket=null;
    public Client_Connection(Socket m_Socket)
    {
    x_Socket=m_Socket;
    System.out.println("创建发送数据流....");
    try
    {
    //out=new BufferedWriter(new OutputStreamWriter(x_Socket.getOutputStream()));
    out = x_Socket.getOutputStream();  //修改
    this.start();
    }
    catch(IOException e1){e1.toString();}
    catch(Exception e){e.toString();}
    }
    public void run()
    {
    String result="终于成功了............";
    System.out.println("开启发送线程....");
    while(true)
    {
    try
    {

     //out.write(result,0,100);  //该句有问题?
     out.write((result+"\r\n").getBytes("gb2312")); //修改
     out.flush();
     System.out.println(result);
    }
    catch(IOException e1){e1.toString();}
    catch(Exception e){e.toString();}
    }
    }
    }