无法将数据从我的"sender.java"发送到我的"receiver.java"。(Java 套接字编程)



我的两个java程序之间的通信出现问题。问题是充当服务器的"receiver.java"程序无法运行/启动或从"sender.java"程序接收数据。发送器程序在端口4444上运行良好。我收到Java套接字连接错误,所以它与连接有关。

最终更新版本*****:现在工作正常,我通过更改端口以匹配解决了第一个问题。(4444(。我通过(IP,4444(NOT(localhost,4444。谢谢大家!

  1. Check_Sum_Sender.java
// Java code for Checksum_Sender 
package checksum_sender; 
import java.io.*; 
import java.net.*; 
import java.util.*; 
public class Checksum_Sender  
{ 
// Setting maximum data length 
private int MAX = 100; 
// initialize socket and I/O streams 
private Socket socket = null; 
private ServerSocket servsock = null; 
private DataInputStream dis = null; 
private DataOutputStream dos = null; 
public Checksum_Sender(int port) throws IOException 
{ 
servsock = new ServerSocket(port); 
// Used to block until a client connects to the server 
socket = servsock.accept(); 
dis = new DataInputStream(socket.getInputStream()); 
dos = new DataOutputStream(socket.getOutputStream()); 
while (true)  
{ 
int i, l, sum = 0, nob; 
Scanner sc = new Scanner(System.in); 
System.out.println("Enter data length"); 
l = sc.nextInt(); 
// Array to hold the data being entered 
int data[] = new int[MAX]; 
// Array to hold the complement of each data 
int c_data[] = new int[MAX]; 
System.out.println("Enter data to send"); 
for (i = 0; i < l; i++)  
{ 
data[i] = sc.nextInt(); 
// Complementing the entered data 
// Here we find the number of bits required to represent 
// the data, like say 8 requires 1000, i.e 4 bits 
nob = (int)(Math.floor(Math.log(data[i]) / Math.log(2))) + 1; 
// Here we do a XOR of the data with the number 2^n -1, 
// where n is the nob calculated in previous step 
c_data[i] = ((1 << nob) - 1) ^ data[i]; 
// Adding the complemented data and storing in sum 
sum += c_data[i]; 
} 
// The sum(i.e checksum) is also sent along with the data 
data[i] = sum; 
l += 1; 
System.out.println("Checksum Calculated is : " + sum); 
System.out.println("Data being sent along with Checkum....."); 
// Sends the data length to receiver 
dos.writeInt(l); 
// Sends the data one by one to receiver 
for (int j = 0; j < l; j++) 
dos.writeInt(data[j]); 
// Displaying appropriate message depending on feedback received 
if (dis.readUTF().equals("success"))  
{    
System.out.println("Thanks for the feedback!! Message received Successfully!"); 
break; 
} 
else if (dis.readUTF().equals("failure"))  
{ 
System.out.println("Message was not received successfully!"); 
break; 
} 
} 
// Closing all connections 
dis.close(); 
dos.close(); 
socket.close(); 
} 
// Driver Method 
public static void main(String args[]) throws IOException 
{ 
Checksum_Sender cs = new Checksum_Sender(4444); 
} 
} 
  1. Check_Sum_Receiver.java
// Java code for Checksum_Receiver 
package checksum_sender; 
import java.net.*; 
import java.io.*; 
import java.util.*; 
public class Checksum_Receiver { 
// Initialize socket and I/O streams 
private Socket s = null; 
private DataInputStream dis = null; 
private DataOutputStream dos = null; 
// Constructor to put ip address and port 
public Checksum_Receiver(InetAddress ip,int port)throws IOException 
{ 
// Opens a socket for connection 
s = new Socket(ip,port); 
dis = new DataInputStream(s.getInputStream()); 
dos = new DataOutputStream(s.getOutputStream()); 
while (true) 
{   Scanner sc = new Scanner(System.in); 
int i, l, nob, sum = 0, chk_sum; 
// Reads the data length sent by sender 
l = dis.readInt(); 
// Initializes the arrays based on data length received 
int c_data[] = new int[l]; 
int data[] = new int[l]; 
System.out.println("Data received (alond with checksum) is"); 
for(i = 0; i< data.length; i++) 
{    
// Reading the data being sent one by one 
data[i] = dis.readInt(); 
System.out.println(data[i]); 
// Complementing the data being received 
nob = (int)(Math.floor(Math.log(data[i]) / Math.log(2))) + 1;  
c_data[i] = ((1 << nob) - 1) ^ data[i]; 
// Adding the complemented data 
sum += c_data[i]; 
} 
System.out.println("Sum(in ones complement) is : "+sum); 
// Complementing the sum 
nob = (int)(Math.floor(Math.log(sum) / Math.log(2))) + 1;  
sum = ((1 << nob) - 1) ^ sum; 
System.out.println("Calculated Checksum is : "+sum); 
// Checking whether final result is 0 or something else 
// and sending feedback accordingly  
if(sum == 0) 
{    
dos.writeUTF("success"); 
break; 
}      
else
{    
dos.writeUTF("failure"); 
break; 
} 
} 
// Closing all connections 
dis.close(); 
dos.close(); 
s.close(); 
} 
// Driver Method 
public static void main(String args[])throws IOException 
{    
// Getting ip address on which the receiver is running 
// Here, it is "localhost" 
InetAddress ip = InetAddress.getLocalHost(); 
Checksum_Receiver cr = new Checksum_Receiver(ip,4444); 
}     
} 

对于任何读过这篇文章并发现这个答案对他们有帮助的人。以下是解决我问题的方法:

1:确保发送器和接收器程序的端口号匹配(发送方:端口=接收方:端口(

2:使用您的本地IP地址作为发送方(127.0.0.1:port(,并使用[本地主机:端口]作为接收方!反之亦然。

3:然后,对于数据输入,首先启动发送器程序,但还没有输入任何内容,然后启动接收器,以便连接数据流。然后输入你喜欢的数据!

最新更新