我正在尝试使用Java Server和C Client实现TCP协议。从客户端读取第一个输入时,代码可以正常正常工作。但是,从客户端的第二次输入开始,即使我尝试使用line = line.replaceAll(“ \ s”,“”)删除空格,服务器也会读取“ hi”或“ command1”。这些不是空格吗?是否有从C读取到Java的垃圾?现在,在我发送第二条命令之后,该程序被阻止,因为服务器无法识别带有这些空格的命令,也无法将任何内容发送回客户端。非常感谢您的帮助或建议,谢谢您的宝贵时间。
以下是我的Java服务器代码:
// A Java program for a Server
import java.net.*;
import java.io.*;
public class HXServerJava
{
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
// constructor with port
public HXServerJava(int port)
{
// starts server and waits for a connection
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
BufferedReader in= new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
String line = "";
// reads message from client until "over" is sent
while (!line.equals("over"))
{
try
{
line = in.readLine();
System.out.println("line before is: " + line);
line = line.replaceAll("\\s",""); //trying to get rid of whitespace
System.out.println("line after is: " + line);
if ((line != null && line.equals("hi")==true)) {
out.print("Welcome by server\n");
out.flush();
//out.close();
}
if ((line != null && line.equals("command1")==true)) {
out.print("first command executed\n");
out.flush();
//out.close();
}
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");
out.print("exit\n");
out.flush();
// close connection
socket.close();
in.close();
out.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
HXServerJava server = new HXServerJava(5000);
}
}
以下是我的C客户端代码:
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define MAX 80
#define PORT 5000
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n,m;
char *str;
int exitflag = 0;
str = malloc(sizeof (char) * MAX);
while (exitflag != 1){
bzero(buff,sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
//strcpy (str,buff);
//strcat(str,"\n");
printf("String is: %s\n",buff);
m = write(sockfd,buff,sizeof(buff));
if (m < 0){
perror ("ERROR writing to socket\n");
}
bzero(buff,sizeof(buff));
m = read(sockfd,sizeof(buff));
if (n < 0) {
perror("ERROR reading from socket\n");
}
printf("From Server : %s\n",buff);
if ((strncmp(buff,"exit",4)) == 0) {
exitflag = 1;
printf("Client Exit...\n");
break;
}
}
}
int main()
{
int sockfd,connfd;
struct sockaddr_in servaddr,cli;
// socket create and varification
sockfd = socket(AF_INET,SOCK_STREAM,0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr,sizeof(servaddr));
// assign IP,PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(sockfd,(SA*)&servaddr,sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
// function for chat
func(sockfd);
// close the socket
close(sockfd);
}