java.net Socket

转载 2012年03月29日 21:02:37
import java.io.*; 
import java.net.*; 

public class Client 
{ 
Socket socket; 
BufferedReader in; 
PrintWriter out; 

public Client() 
{ 
try 
{ 
socket = new Socket("127.0.0.1", 10000); 
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

out = new PrintWriter(socket.getOutputStream(),true); 
BufferedReader line = new BufferedReader(new InputStreamReader(System.in)); 

out.println(line.readLine());
out.flush();
System.out.println(in.readLine());
//out.println("Hello Server!\n");
line.close(); 
out.close(); 

//
in.close(); 
socket.close(); 
} 
catch (IOException e) 
{} 
} 

public static void main(String[] args) 
{ 
new Client(); 
} 
} 


/*
 *Server: Main.java
 *
 * ServerDemo Java application
 *
 * Created on 29-03-2012 08:01 PM
 */
import java.net.*; 
import java.io.*; 

class Server 
{ 
private ServerSocket ss; 
private Socket socket; 
private BufferedReader in; 
private PrintWriter out; 

public Server() 
{ 
try 
{ 
ss = new ServerSocket(10000); 

while (true) 
{ 
socket = ss.accept(); 
System.out.println("建立新连接\n");
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
//out = new PrintWriter(socket.getOutputStream(),true);
out = new PrintWriter(socket.getOutputStream());

String line = in.readLine(); 
System.out.println("you input is :" + line); 
out.println("you input is :" + line); 
out.flush();
out.close(); 
in.close(); 
socket.close(); 
} 
//ss.close(); 
} 
catch (IOException e) 
{System.out.println(e.getMessage()); } 

} 
/*
public static void main(String[] args) 
{ 

} 
*/
} 

public class Main{
    public static void main(String[] args) {
		new Server(); 
    }
}

相关文章推荐

java.net.SocketException: Connection reset 关于socket连接异常

第1个异常是java.net.BindException:Address already in use: JVM_Bind。该异常发生在服务器端进行new ServerSocket(port)(por...

Intellj IDEA14上用Debug启动项目启动不了:Unable to open debugger port: java.net.SocketException "socket closed"

[已解决:有应该断点没去掉]Unable to open debugger port: java.net.SocketException "socket closed" 网上说要,重启电脑IDEA才可...

30. Socket下载时,服务器端出错: len=10240 count=115 len=10240 count=116 len=7168 count=last java.net.SocketExc

问题: 30. Socket下载时,服务器端出错: len=10240 count=115 len=10240 count=116 len=7168 count=last java.ne...

在android 中开发java.net.SocketException: socket failed: EACCES (Permission denied) 报错

在android 中开发java.net.SocketException: socket failed: EACCES (Permission denied) 报错 知识点: 1、SocketEx...

java.net.Socket

public class Socket extends Object implements Closeable         类简介:         这...

Caused by: java.net.SocketException: Software caused connection abort: socket write error

1、错误描述[ERROR:]2015-10-16 22:28:39,964 [异常拦截] exception.ExceptionHandler ClientAbortException: java....

centOS7.0 下配置Hadoop集群,Slave1报错:failed on socket timeout exception: java.net.NoRouteToHostException

 Hadoop版本:2.5.0 在配置Hadoop集群时,在Master上 启动目录/usr/hadoop/sbin/下的./start-all.sh后,在Master主机上 [hadoop...

ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error"异常原因分析

在网上查找了了下原因,大概归结为:  ClientAbortException: java.net.SocketException: Connection reset by peer: socket...

网络编程2之Socket简介和java.net包

socket简介 socket通信原理 Java.net包下的具体类和方法

java和.net的socket通信

  • 2014-04-25 09:45
  • 23KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)