java端
package ImageService;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.sound.sampled.AudioFormat.Encoding;
import sun.misc.BASE64Encoder;
public class testsend {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 9999);
//code to send image
// byte[] fileBytes = new byte[(int)file.length()];
//wqj
String imgFile = "d:\\116.109009,39.537940,116.114502,39.542176.jpg";//待处理的图片
InputStream inPut = s.getInputStream();
OutputStream outPut = s.getOutputStream();
File file = new File(imgFile);
byte[] outBytes = new byte[1024];
int count = 0;
FileInputStream fileInput = new FileInputStream(file);
ByteArrayOutputStream ow = new ByteArrayOutputStream();
while ((count = fileInput.read(outBytes)) > 0) {
ow.write(outBytes, 0, count);
}
outPut.write(ow.toByteArray());
//System.out.println("file size is "+ fileBytes.length);
// for(byte a : fileBytes){System.out.println(a);}
inPut.close();
outPut.close();
fileInput.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
c#端
static void Main(string[] args)
{
Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint hostIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
//设置接收数据缓冲区的大小
byte[] b = new byte[2048];
receiveSocket.Bind(hostIpEndPoint);
//监听
receiveSocket.Listen(2);
Console.WriteLine("已经开始监听");
Socket hostSocket = receiveSocket.Accept();
byte[] buffer = new byte[1024];
int recCount = 0;
MemoryStream memStream = new MemoryStream();
while ((recCount = hostSocket.Receive(buffer)) > 0)
{
memStream.Write(buffer, 0, recCount);
}
Image image = Image.FromStream(memStream);
image.Save("D:\\111.jpg");
hostSocket.Shutdown(SocketShutdown.Receive);
hostSocket.Close();
//关闭发送连接
receiveSocket.Close();
}