我想制作一个简单的客户端-服务器应用程序,服务器将其发送到客户端文件(照片)。我在C中使用服务器,在Java中使用客户端。
server.c
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <pthread.h>
struct sockaddr_in c_addr;
char fname[100];
void* SendFileToClient(int *arg )
{
int connfd=(int)*arg;
printf("Connection accepted and id: %d\n",connfd);
printf("Connected to Clent: %s:%d\n",inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port));
write(connfd, fname,256);
FILE *fp = fopen(fname,"rb.jpg");
if(fp==NULL)
{
printf("File opern error");
return 1;
}
/* Read data from file and send it */
while(1)
{
/* First read file in chunks of 256 bytes */
unsigned char buff[1024]={0};
int nread = fread(buff,1,1024,fp);
//printf("Bytes read %d \n", nread);
if(nread > 0)
{
write(connfd, buff, nread);
}
if (nread < 1024)
{
if (feof(fp))
{
printf("End of file\n");
printf("File transfer completed for id: %d\n",connfd);
}
if (ferror(fp))
printf("Error reading\n");
break;
}
}
printf("Closing Connection for id: %d\n",connfd);
close(connfd);
shutdown(connfd,SHUT_WR);
sleep(2);
}
int main(int argc, char *argv[])
{
int connfd = 0,err;
pthread_t tid;
struct sockaddr_in serv_addr;
int listenfd = 0,ret;
char sendBuff[1025];
int numrv;
size_t clen=0;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd<0)
{
printf("Error in socket creation\n");
exit(2);
}
printf("Socket retrieve success\n");
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(8887);
ret=bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr));
if(ret<0)
{
printf("Error in bind\n");
exit(2);
}
if(listen(listenfd, 10) == -1)
{
printf("Failed to listen\n");
return -1;
}
strcpy(fname,"150.254.79.154");
while(1)
{
clen=sizeof(c_addr);
printf("Waiting...\n");
connfd = accept(listenfd, (struct sockaddr*)&c_addr,&clen);
if(connfd<0)
{
printf("Error in accept\n");
continue;
}
err = pthread_create(&tid, NULL, &SendFileToClient, &connfd);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
}
close(connfd);
return 0;
}
客户端
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
public class client2 {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("150.254.79.87", 8887);
InputStream inputStream = socket.getInputStream();
byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
byte[] imageAr = new byte[size];
inputStream.read(imageAr);
BufferedImage image = ImageIO.read(socket.getInputStream());
ImageIO.write(image, "jpg", new File(""));
socket.close();
}
}
问题在于客户端冻结在:
BufferedImage image = ImageIO.read(socket.getInputStream());
我在调试中检查了客户端,图像= null。
我尝试其他方法:
Socket socket = new Socket("150.254.79.154", 8887);
byte[] contents = new byte[10000];
FileOutputStream fos = new FileOutputStream("(filepath)");
BufferedOutputStream bos = new BufferedOutputStream(fos);
InputStream is = socket.getInputStream();
BufferedImage image = ImageIO.read(is);
ImageIO.write(image, "jpg", fos);
我得到了:
Exception in thread "main" java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
at javax.imageio.ImageIO.getWriter(ImageIO.java:1592)
at javax.imageio.ImageIO.write(ImageIO.java:1578)
at client2.main(client2.java:38)
我在C客户端中进行了检查-一切都很好,服务器可以正常工作。现在,我想加入Java服务器,但是如您所见,我遇到了问题。 :/