关于socket
2013-10-24 11:03
148 查看
由于网络上的socket举例在本人看来并不是最简单的socket举例,所以我给出以下举例。
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public void main(String[] args){
try {
ServerSocket serverSocket=new ServerSocket(4700);
Socket socket=serverSocket.accept();
InputStream is=socket.getInputStream();
byte[] b=new byte[1024];
is.read(b);
String getStr=new String(b);
System.out.println(getStr);
}catch (Exception e){
e.printStackTrace();
}
}
}
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args){
try {
Socket socket=new Socket("192.168.0.6",4700);
Scanner scan=new Scanner(System.in);
OutputStream os=socket.getOutputStream();
os.write(scan.nextLine().getBytes("utf-8"));//由于我使用的是centos作为服务端,其编码格式为utf-8,为了避免乱码,所以使用这个编码格式转换成字节流
os.close();
scan.close();
socket.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
socket网络上传单个文件
public class Client1 {
public Socket socket;
public Client1(){
try {
socket=new Socket("192.168.0.6",4700);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Client1(String intelAddr,int port){
try {
socket=new Socket(intelAddr,port);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean sendFile(String fileName){
OutputStream os=null;
try {
os=socket.getOutputStream();
boolean bl1=sendFileName(fileName,os);
boolean bl2=sendFile(fileName,os);
return bl1&&bl2;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally{
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean sendFileName(String fileName,OutputStream os){
String[] fileNames=fileName.split("[\\/]");
fileName=fileNames[fileNames.length-1];
try {
os.write(fileName.getBytes("utf-8"));
os.flush();
return true;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public boolean sendFile(String fileName,OutputStream os){
File f=new File(fileName);
try {
FileInputStream fis=new FileInputStream(f);
byte[] b=new byte[1024];
int len=0;
while((len=fis.read(b))>0){
os.write(b, 0, len);
}
os.flush();
fis.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args){
try {
ServerSocket ss=new ServerSocket(4700);
Socket socket=ss.accept();
InputStream is=socket.getInputStream();
byte[] b=new byte[1024];
int len=0;
is.read(b);
String fileName=new String(b);
File newFile=new File(fileName);
newFile.mkdirs();
FileOutputStream fos=new FileOutputStream("/"+newFile);
while((len=is.read(b))>0){
fos.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public void main(String[] args){
try {
ServerSocket serverSocket=new ServerSocket(4700);
Socket socket=serverSocket.accept();
InputStream is=socket.getInputStream();
byte[] b=new byte[1024];
is.read(b);
String getStr=new String(b);
System.out.println(getStr);
}catch (Exception e){
e.printStackTrace();
}
}
}
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args){
try {
Socket socket=new Socket("192.168.0.6",4700);
Scanner scan=new Scanner(System.in);
OutputStream os=socket.getOutputStream();
os.write(scan.nextLine().getBytes("utf-8"));//由于我使用的是centos作为服务端,其编码格式为utf-8,为了避免乱码,所以使用这个编码格式转换成字节流
os.close();
scan.close();
socket.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
socket网络上传单个文件
public class Client1 {
public Socket socket;
public Client1(){
try {
socket=new Socket("192.168.0.6",4700);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Client1(String intelAddr,int port){
try {
socket=new Socket(intelAddr,port);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean sendFile(String fileName){
OutputStream os=null;
try {
os=socket.getOutputStream();
boolean bl1=sendFileName(fileName,os);
boolean bl2=sendFile(fileName,os);
return bl1&&bl2;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally{
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean sendFileName(String fileName,OutputStream os){
String[] fileNames=fileName.split("[\\/]");
fileName=fileNames[fileNames.length-1];
try {
os.write(fileName.getBytes("utf-8"));
os.flush();
return true;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public boolean sendFile(String fileName,OutputStream os){
File f=new File(fileName);
try {
FileInputStream fis=new FileInputStream(f);
byte[] b=new byte[1024];
int len=0;
while((len=fis.read(b))>0){
os.write(b, 0, len);
}
os.flush();
fis.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args){
try {
ServerSocket ss=new ServerSocket(4700);
Socket socket=ss.accept();
InputStream is=socket.getInputStream();
byte[] b=new byte[1024];
int len=0;
is.read(b);
String fileName=new String(b);
File newFile=new File(fileName);
newFile.mkdirs();
FileOutputStream fos=new FileOutputStream("/"+newFile);
while((len=is.read(b))>0){
fos.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章推荐
- 关于错误:ORA-12592: TNS: 包错误 和 Io 异常: Connection reset by peer: socket write error
- 关于socket send recv 两个函数各种错误说明
- 关于socket编程
- socket关于数据边界的说明
- 关于:socket阻塞、非阻塞,同步、异步、I/O模型
- 关于socket的Accept阻塞模式的退出方法分析
- 关于Socket通信服务的心跳包
- 关于socket通信时传输对象
- 关于socket网络编程,各种报错若干总结
- Boost.Asio和ACE之间关于Socket编程的比较
- Boost.Asio和ACE之间关于Socket编程的比较
- 看到关于socket非阻塞模式设置方式记录一下。
- 关于 Out of Socket memory 的解释
- 关于socket tcp 断线重连
- 关于socket里chunk问题的解决
- 关于socket tcp 断线重连
- 关于socket_create 报错,call to undefined function socket_create()的可能的解决方案。
- 关于创建socket失败的问题
- linux下关于socket程序的调试(gdb)
- 关于Silverlight的Socket 应用